0
\$\begingroup\$

My code below is modeled after Unity's documentation. When I put the script into an empty GameObject, the error I get is:

MissingComponentException: There is no 'MeshFilter' attached to the "myMesh" game object, but a script is trying to access it.

How do I add a MeshFilter (and the other necessary Components) to the empty GameObject in order to generate the model procedurally?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class myMesh : MonoBehaviour  {
    public Mesh mesh;

    void Start(){
        mesh= new Mesh();
        GetComponent<MeshFilter>().mesh = mesh;
    }

}
\$\endgroup\$
1

1 Answer 1

0
\$\begingroup\$

You can use the AddComponent method to add a new component of your choosing:

var filter = AddComponent<MeshFilter>();
filter.mesh = mesh;

var renderer = AddComponent<MeshRenderer>();
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .