Setting mesh.vertices to a 3-dimensional array

Ello everyone! this is my first time posting in this forum.

Anyways, I have a code that I wrote, of which I plan to create a voxel terrain from.

I have some 3d arrays set up, so that it can be simpler to generate a “sponge” of points. In other words, I will not only have points on the outside of the mesh, but filling the inside as well, so as to later be able to just switch them on and off. My plan is that this will be used to decide which vertices are triangulated with other adjacent vertices.

Alright, to my point.

I have a code here, and I have assigned the vertices, uvs and tris to the mesh like so -

using UnityEngine;
using System.Collections;

public class Voxel : MonoBehaviour {

	//initializes the size changing values
	public int WidthX;
	public int WidthY;
	public int WidthZ;

	//initializes the arrays
	public Vector3[,,] Verts;
	public Vector2[,,] UVs;
	public int[,,] Tris;

	void Start () {

		//sets the voxel size
		WidthX = 5;
		WidthY = 10;
		WidthZ = 5;

		//applies the voxelmesh to the game object
		MeshFilter mf = GetComponent<MeshFilter>();
		Mesh vox = new Mesh();
		mf.mesh = vox;
		vox = GetComponent<MeshFilter>().mesh;
		vox.Clear();

		//set vertices, uvs, and tris here
		Verts = new Vector3[WidthX,WidthY,WidthZ];

		UVs = new Vector2[WidthX,WidthY,WidthZ];

		Tris = new int[WidthX*3,WidthY*3,WidthZ*3];

		//apply values to mesh here
		vox.vertices = Verts;
		vox.uv = UVs;
		vox.triangles = Tris;

		vox.RecalculateNormals();
		vox.RecalculateBounds();

	}
}

So, when I hit F8 to build it, it says “Cannot convert Vector3 to Vector3[,]”.
No errors before I assign them, so I know that the 3d arrays exist and work. My problem is the
compatibility.

Any suggestions?

Mesh.vertices, Mesh.uv, and Mesh.triangles are all one dimensional arrays, so you are going going to have to unwind your 3D arrays somehow. Iterating over them and building the 1D array is probably the most efficient, but you can have simpler code using LinQ:

In addition, if I understand what you are doing correctly, you’ve sized your triangles array incorrectly. You will have two triangles (three entries each) for every four vertices. So the number of entries will be:

  no. vertices / 4 * 2 * 3