Cube Terrain Generation: For loop creating duplicates

Well im not sure why this is happening but when this for loop finishes on z, it changes it self from 10 to 0 and acts like it is its another valid loop when it should stop at 10 to make 100 blocks but instead its making a extra block for each time the z loop is executed therefore throwing a IOOR Exception

Here is the code

public int mapX;
public int mapZ;

public int mapMax = 50;

public int mapMinimum = 250;

private GameObject cubes;
private GameObject terrain;
private int cube = 0;

void Start () {
mapX = 10;
mapZ = mapX;

cubes = new GameObject[mapX * mapZ];

for (int x = 0; x <= mapX; x += 1) {
Debug.Log("cubes: " + cubes.Length + " cube: " + cube);
cubes[cube] = GameObject.CreatePrimitive(PrimitiveType.Cube);
cubes[cube].transform.localPosition = new Vector3(x, 0, 0);
cubes[cube].name = "X: " + cubes[cube].transform.localPosition.x + " y: " + cubes[cube].transform.localPosition.y + " Z: " + cubes[cube].transform.localPosition.z;
cubes[cube].transform.localScale = new Vector3(1, 1, 1);
cube += 1;
for (int z = 0; z <= mapZ; z += 1) {
Debug.Log("cubes: " + cubes.Length + " cube: " + cube);
cubes[cube] = GameObject.CreatePrimitive(PrimitiveType.Cube);
cubes[cube].transform.localPosition = new Vector3(x, 0, z);
cubes[cube].name = "X: " + cubes[cube].transform.localPosition.x + " y: " + cubes[cube].transform.localPosition.y + " Z: " + cubes[cube].transform.localPosition.z;
cubes[cube].transform.localScale = new Vector3(1, 1, 1);
cube += 1;
}
}
}

Here is the result

The chunk of code in just the x loop is extraneous. You’re creating a cube at x,0,0 every time x increments when you shouldn’t be.

for (int x = 0; x <= mapX; x += 1) {
for (int z = 0; z <= mapZ; z += 1) {
Debug.Log("cubes: " + cubes.Length + " cube: " + cube);
cubes[cube] = GameObject.CreatePrimitive(PrimitiveType.Cube);
cubes[cube].transform.localPosition = new Vector3(x, 0, z);
cubes[cube].name = "X: " + cubes[cube].transform.localPosition.x + " y: " + cubes[cube].transform.localPosition.y + " Z: " + cubes[cube].transform.localPosition.z;
cubes[cube].transform.localScale = new Vector3(1, 1, 1);
cube += 1;
}
}

Oh yea… this is what happens when you code too much with java :confused:

Why do you spawn cubes…
It renders way more than needed.
You should seriously use voxel solution instead o_o

Ok i did that and this is the result

That is what im doing but im just using cubes for now so i can easily transfer from simple to advanced