Weird result with for loop

Hi
i am trying to make a 3x3x3 cube of smaller cubes using 3 nested loop but i am getting weird results .
Here is the code :


#pragma strict

var cubemain : Transform; //a cube that exist in the scene

function Start ()
{

var maxL: int = 3;
var maxW: int = 3;
var maxH: int = 3;

for ( var x:int =0;x < maxL; x++ )
{
for ( var y:int = 0;y < maxH; y++ )
{
for ( var z:int = 0;z < maxW;z++ )
{
Instantiate(cubemain, Vector3 (x, y, z), Quaternion.identity);
}

Instantiate(cubemain, Vector3 (x, y, z), Quaternion.identity);
}
Instantiate(cubemain, Vector3 (x, y, z), Quaternion.identity);
}
}


Instead of getting the 3x3x3 cube i am geting something like this a 3x3x4 and another row on top at one of the edges .
looks something like this :
1456862--79105--$Capture.PNG
From what i can see is that for some reason the loops are not executed as they should .
I have tried using while loops , no go .

Before you ask , i tried using a prefab and unity hangs .
Tried this on Unity pro trial and on Unity free version same result .
Did this on 2 different computers , with AMD processors and windows 7 .

Am i doing something wrong or is this a Unity compatibility problem ?

Thanks in advance for your help .

You’re creating ton many boxes :slight_smile: Try this one:

var maxL: int = 3;
var maxW: int = 3;
var maxH: int = 3;

for ( var x:int =0;x < maxL; x++ )
{
  for ( var y:int = 0;y < maxH; y++ )
  {
    for ( var z:int = 0;z < maxW;z++ )
    {
        Instantiate(cubemain, Vector3 (x, y, z), Quaternion.identity);
    }
  }
}

The instantiation in the outer loops aren’t neccessary.

Thx
Are right , it came to me a few minutes after i wrote the post that it might be that i am doing something wrong in one of the loops .
But you know how it is sometimes you just need a fresh pair of eyes to look over the code .

Thank you very much it works great .