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;


1 Answer 1


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>();

You must log in to answer this question.

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