Vectors in mesh.vertices always zero?

I’m trying to procedurally generate a mesh with a size of sizeX by sizeZ squares (where sizeX and sizeZ are user-defined integers). Each square is built up out of two triangles, which consist of three connected vertices.

I have worked out all the logic on how to generate all the required vertices, and enter them into the mesh.triangles array in the correct order. However, one thing has been blocking me from moving forward for a while now. It’s this piece of code:

int i = 0;
for( int z = 0 ; z <= sizeZ ; z++ )
{
	for( int x = 0 ; x <= sizeX ; x++ )
	{
		// places vertex (x increased by 2f for testing purposes)
		mesh.vertices *= new Vector3( x+2f, 0f, z );*
  •  // always prints (0.0, 0.0, 0.0)*
    

_ Debug.Log( mesh.vertices );_
* i++;*
* }*
}
Apparently the vertices aren’t being set properly? I have checked everything, and someone else has successfully run the code without any problems, but for some reason it’s just not setting the vertices on my end.
If anyone could shine a light on what is happening here, that would be great. Any other pointers or things I could try would be appreciated as well.
If needed, you can find the full script [here][1] (also includes pseudo-code and ascii art to explain the logic behind this), and download the project files [here][2] (PerlinMesh2D.cs is the script, but I thought I’d include the others as well just in case).
Hope someone can help me out with this. Thanks!
_[1]: http://pastebin.com/WHpbBjF7*_
[2]: http://fang.io/files/Procedural_Practice.zip*

1 Answer

1

I think you are supposed to get a copy of the mesh vertices array, or create a new one, then assign it back to the mesh like so.

Vector3[] vertices = mesh.vertices;  // or = new Vector3[4];

// make your changes to your array
vertices *= ...*

// when done, assign vertices array to mesh
mesh.vertices = vertices;
mesh.RecalculateBounds();

After doing the same for the triangles, that totally worked! Never in my life have I been more happy with an eyesore-pink square. Thank you so much!

My pleasure, glad it worked.

If you are going to assign the whole array back, I believe it is good practice to call mesh.Clear() before reassigning vertices; then recalculate the bounds.