I can create a mesh (in awake) but I cannot see it

Viewed 35

this is the part of the code that generate a mesh, if you want more information ask. So in the scene I cannot see it, I tried to enter play mode but nothing, if I go in the inspector I can see: 4 verts, 2 tris, the right amount of tris and verts.

 public void ConstructMesh()
    {
        Vector3[] vertices = new Vector3[(resolution + 2) * (resolution + 2)];
        int[] triangles = new int[(resolution + 1) * (resolution + 1) * 6];
        int triIndex = 0;

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int i = x + y * resolution;
                Vector2 percent = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCode = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
                vertices[i] = pointOnUnitCode;

                triangles[triIndex] = i;
                triangles[triIndex + 1] = i + resolution + 1;
                triangles[triIndex + 2] = i + resolution;

                triangles[triIndex + 3] = i;
                triangles[triIndex + 4] = i + 1;

                triangles[triIndex + 5] = i + resolution + 1;
                 triIndex += 6;
            }
        }
        planet.Clear();
        planet.vertices = vertices;
        planet.triangles = triangles;
        planet.RecalculateNormals();
    }
1 Answers

You need a MeshRenderer and MeshFilter in the GameObject to be able to render your mesh.

Supposing your method ConstructMesh is a MonoBehaviour and it's attached to the same object you want to display the mesh, you can do the following:

GetComponent<MeshFilter>().mesh = planet;
Related