When generating a procedural voxel mesh, i get something weird.

Okay, so this is my code

#pragma strict
var ChunkPrefab : GameObject;
var World : int[,,];
var WorldSize : Vector3;
var WorldRandomizer : int; 
var HeightMultiplier : int;
var LastChunk : GameObject;
var LastMesh : Mesh;
var LastFaces : boolean[];
var LastVerts : Array;
var LastTriangles : Array;
var VertsAmount : int;
function Create () {
	WorldRandomizer = Random.Range(0, 999999);
	World = new int[WorldSize.x, WorldSize.y, WorldSize.z];
	for(var x : float = 0; x < WorldSize.x; x++) {
		for(var z : float = 0; z < WorldSize.z; z++) {
			World[x, PerlinCalculations(x, z), z] = 1;
		}
	}
	LoadChunk(0, 0);
}
function PerlinCalculations (x : float, z: float) {
	return(Mathf.RoundToInt(Mathf.PerlinNoise(WorldRandomizer+x/HeightMultiplier, WorldRandomizer+z/HeightMultiplier)*HeightMultiplier));
}
function LoadChunk(x : double, z : double) {
	// 0 top
	// 1 front
	// 2 bot
	// 3 back
	// 4 left
	// 5 right
	LastChunk = GameObject.Instantiate(ChunkPrefab);
	LastChunk.transform.position = new Vector3(x*8, z*8);
	LastMesh = LastChunk.GetComponent.<MeshFilter>().mesh;
	LastVerts = new Array ();
	LastTriangles = new Array ();
	VertsAmount = 0;
	for(var cx : int = 0; cx < 8; cx++) {
		for(var cz : int = 0; cz < 8; cz++) {
			for(var cy : int = 0; cy < WorldSize.y; cy++) {
				if(World[cx + x, cy, cz + z] == 1) {
					LastFaces = new boolean[6];
					//World edge faces
					if(cy == WorldSize.y - 1) {
						LastFaces[0] = true;
					}
					else {
						if(World[cx + x, cy + 1, cz + z] == 1) {} else {
							LastFaces[0] = true;
						}
					}
					if(cx == 0) {
						Debug.Log("Test");
						LastFaces[1] = true;
					}
					else {
						if(World[cx + x - 1, cy, cz + z] == 1) {} else {
							LastFaces[1] = true;
						}
					}
					if(cy == 0) {
						LastFaces[2] = true;
					}
					else {
						if(World[cx + x, cy - 1, cz + z] == 1) {} else {
							LastFaces[2] = true;
						}
					}
					if(cx == WorldSize.x - 1) {
						LastFaces[3] = true;
					}
					else {
						if(World[cx + x + 1, cy, cz + z] == 1) {} else {
							LastFaces[3] = true;
						}
					}
					if(cz == 0) {
						LastFaces[4] = true;
					}
					else {
						if(World[cx + x, cy, cz + z - 1] == 1) {} else {
							LastFaces[4] = true;
						}
					}
					if(cz == WorldSize.z - 1) {
						LastFaces[5] = true;
					}
					else {
						if(World[cx + x, cy, cz + z + 1] == 1) {} else {
							LastFaces[5] = true;
						}
					}
					CreateVox(new Vector3(cx + x, cy, cz + z));
				}
			}
		}
	}
	LastMesh.Clear ();
	LastMesh.vertices = LastVerts;
	LastMesh.triangles = LastTriangles;
	LastMesh.Optimize ();
	LastMesh.RecalculateNormals();
}
function CreateVox (Place : Vector3) {
	LastVerts.Add(new Vector3(Place.x + 0, Place.y + 0, Place.z + 0));
	LastVerts.Add(new Vector3(Place.x + 0, Place.y + 1, Place.z + 0));
	LastVerts.Add(new Vector3(Place.x + 1, Place.y + 1, Place.z + 0));
	LastVerts.Add(new Vector3(Place.x + 1, Place.y + 0, Place.z + 0));
	LastVerts.Add(new Vector3(Place.x + 0, Place.y + 0, Place.z + 1));
	LastVerts.Add(new Vector3(Place.x + 0, Place.y + 1, Place.z + 1));
	LastVerts.Add(new Vector3(Place.x + 1, Place.y + 1, Place.z + 1));
	LastVerts.Add(new Vector3(Place.x + 1, Place.y + 0, Place.z + 1));
	if(LastFaces[0]) {
		LastTriangles.Add(2+VertsAmount);
		LastTriangles.Add(1+VertsAmount);
		LastTriangles.Add(5+VertsAmount);
		LastTriangles.Add(6+VertsAmount);
		LastTriangles.Add(2+VertsAmount);
		LastTriangles.Add(5+VertsAmount);
	}
	if(LastFaces[1]) {
		LastTriangles.Add(0+VertsAmount);
		LastTriangles.Add(1+VertsAmount);
		LastTriangles.Add(2+VertsAmount);
		LastTriangles.Add(2+VertsAmount);
		LastTriangles.Add(3+VertsAmount);
		LastTriangles.Add(0+VertsAmount);
	}
	if(LastFaces[2]) {
		LastTriangles.Add(0+VertsAmount);
		LastTriangles.Add(3+VertsAmount);
		LastTriangles.Add(4+VertsAmount);
		LastTriangles.Add(3+VertsAmount);
		LastTriangles.Add(7+VertsAmount);
		LastTriangles.Add(4+VertsAmount);
	}
	if(LastFaces[3]) {
		LastTriangles.Add(6+VertsAmount);
		LastTriangles.Add(5+VertsAmount);
		LastTriangles.Add(7+VertsAmount);
		LastTriangles.Add(7+VertsAmount);
		LastTriangles.Add(5+VertsAmount);
		LastTriangles.Add(4+VertsAmount);
	}
	if(LastFaces[4]) {
		LastTriangles.Add(5+VertsAmount);
		LastTriangles.Add(1+VertsAmount);
		LastTriangles.Add(4+VertsAmount);
		LastTriangles.Add(4+VertsAmount);
		LastTriangles.Add(1+VertsAmount);
		LastTriangles.Add(0+VertsAmount);
	}
	if(LastFaces[5]) {
		LastTriangles.Add(2+VertsAmount);
		LastTriangles.Add(6+VertsAmount);
		LastTriangles.Add(3+VertsAmount);
		LastTriangles.Add(6+VertsAmount);
		LastTriangles.Add(7+VertsAmount);
		LastTriangles.Add(3+VertsAmount);
	}
	VertsAmount+= 8;
}

ChunkPrefab is just a simple GameObject with a MeshFilter and a MeshRenderer.
And I’m getting something like this:
alt text

Please, help!

The main problem with the code is that you have flipped x and z axis. For instance if 1 is front face then you should be checking World[cx, cy, cz+1] instead of World[cx + x - 1, cy, cz + z]. Allr 4 x/z checks need to be checked.

The strange shading is because in unity normals are per vertex so a vertex that is on 3 faces will have a normal that is the average of the normal of the faces. To fix the shading you should make 4 vertices per face so 4*6 vertices per cube.

Finally you are making alot of vertices that aren’t being used. Since you probably need 4 vertices per face anyways just generate then with the triangles.