- Home /
This question was
closed Jul 03, 2014 at 12:24 PM by
Josh Naylor for the following reason:
Duplicate Question
Question by
madfatcat · Jul 03, 2014 at 12:24 PM ·
meshmeshfilterprimitive
Cannot modify primitive's mesh
Hi All,
I have a problem modifying cube's mesh. I simply put the cube primitive on the scene, then in the script I write:
Mesh aMesh = GetComponent<MeshFilter>().sharedMesh;
aMesh.vertices [0] = aMesh.vertices [1] = aMesh.vertices [2] = aMesh.vertices [3] = new Vector3 (10, 0, 0);
Debug.Log (aMesh.vertices[0].x);
these changes to cube's mesh don't affect the cube's shape and Debug.Log shows another value, not 10. The same is with following code:
Mesh aMesh = GetComponent<MeshFilter>().mesh;
aMesh.vertices [0] = aMesh.vertices [1] = aMesh.vertices [2] = aMesh.vertices [3] = new Vector3 (10, 0, 0);
GetComponent<MeshFilter>().mesh = aMesh;
But when I create the mesh from scratch, like this
Mesh aMesh = new Mesh ();
aMesh.vertices = new Vector3[24] { assigning vertices here };
aMesh.triangles = new int[] { assigning triangles here };
aMesh.uv = new Vector2[24] { assigning UVs here };
aMesh.RecalculateNormals ();
GetComponent<MeshFilter>().mesh = aMesh;
everything works fine, the object looks according to the vertices assigned to the new mesh. Please tell me how to modify an existing mesh of a cube? I miss something really simple but it's my second day with Unity.
Comment
See this thread for help. http://answers.unity3d.com/questions/26894/how-do-change-the-mesh-of-a-game-object.html