Mesh vertices not being set

I’m having some trouble setting the vertices of a mesh. Below is my code:

cubeMesh.vertices = new Vector3[12];
for (int vertexIndex = 0; vertexIndex < cubeMesh.vertices.Length; vertexIndex++) 
   cubeMesh.vertices[vertexIndex] = vertlist[vertexIndex];
   Debug.Log(vertlist[vertexIndex]);
   Debug.Log(cubeMesh.vertices[vertexIndex]);
}

When I run the code, the debugger outputs:
(1.0, 0.0, 0.7)
(0.0, 0.0, 0.0)
(0.3, 0.0, 0.3)
(0.0, 0.0, 0.0)

and so on. How could the adjacent lines be different if I’m setting the value of the mesh’s vertices to the value contained in the vertex list, and then immediately printing their values to the console? Help would be greatly appreciated.

You need to supply Mesh.vertices with a complete array. Mesh.vertices returns a copy of the vertex array and because of this
Mesh.vertices[×] = y;
doesn’t really do anything. You are just modifying the copy and then throwing it away.
Create a new array at the start:
x = new Vector3[y];
Modify it in loop:
x = z;
Then assign it to mesh.
mesh.vertices = x;

1 Like

Thank you! This worked.

If you want a random smattering of other cute procedural generation stuff, check out my MakeGeo package:

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

It might give you some ideas to build on for your stuff.