I declare z = 0; at the top of the page, and then i use it for a built in array length to mark all the positions of instantiated walls in an array.
the instantiation loop says:
instantiate a wall,
z=z++;
print(z);
The loop makes 5 walls… and then z++ 5 times z= 2?!
for ( j = 0; j < neighbours.length; j ++)
{
if ( neighbours[j] != (setmov + stp3 ))
{
var stpc = Instantiate(cubePrefab, setmov + stp3*.5, rot);
stpc.tag=("wall");
stpc.name=("wall");
stpc.transform.localScale=Vector3(1,.1,1);
z=z++;
print(z);
walls = new Vector3[z+1];
print(walls.length);
var newWallPos = (setmov + stp3*.5);
walls[z]=newWallPos;
break;
}
else
here is the complete code:
#pragma strict
var cubePrefab : GameObject ;
private var rot : Quaternion;
rot.eulerAngles = Vector3(0, 0, 0);
var tree : GameObject ;
tree = gameObject ;
private var mov : Vector3;
private var mov2 : Vector3;
private var setmov : Vector3;
private var neighbours : Vector3[];
private var walls : Vector3[];
private var newall : Vector3 ;
var tally = 0;
function Start () {
Lines (tree,1,10);
}
function Lines( iter:GameObject, ratio:float , depth:float)//add line of cubes
{
print("new loop");
print(tally);
neighbours = new Vector3[depth];
walls = new Vector3[tally];
for (var m:int = 0; m < depth; m ++)
{
mov2 = setmov;
var cube = Instantiate(cubePrefab,Vector3(0, 0, 0),rot);
var stp1 = Vector3(1,0,0);
var stp2 = Vector3(-1,0,0);
var stp3 = Vector3(0,1,0);
var stp4 = Vector3(0,-1,0);
var stp5 = Vector3(0,0,1);
var stp6 = Vector3(0,1,0);
var rnd : int = Random.Range(0, 2);
if (rnd==1){mov = stp1;}
if (rnd==0){mov = stp2;}
//if (rnd==0){mov = stp3;}
//if (rnd==3){mov = stp4;}
//if (rnd==4){mov = stp5;}
//if (rnd==5){mov = stp6;}
setmov = mov + mov2;
cube.transform.position = setmov;
for (var j:int = 0; j < neighbours.length; j ++)
{
if ( neighbours[j] == setmov )
{
var dwalls = GameObject.FindGameObjectsWithTag ("wall");
for (var dwall in dwalls)
{
if(dwall.transform.position == (setmov + stp1*.5)){Destroy(dwall);}
if(dwall.transform.position == (setmov + stp2*.5)){Destroy(dwall);}
if(dwall.transform.position == (setmov + stp3*.5)){Destroy(dwall);}
if(dwall.transform.position == (setmov + stp4*.5)){Destroy(dwall);}
if(dwall.transform.position == (setmov + stp5*.5)){Destroy(dwall);}
if(dwall.transform.position == (setmov + stp6*.5)){Destroy(dwall);}
}
break;
}
}
for ( j = 0; j < neighbours.length; j ++)
{
if ( neighbours[j] != (setmov + stp3 ))
{
var stpc = Instantiate(cubePrefab, setmov + stp3*.5, rot);
stpc.tag=("wall");
stpc.name=("wall");
stpc.transform.localScale=Vector3(1,.1,1);
tally=tally++;
print(tally);
walls = new Vector3[tally+1];
print(walls.length);
var newWallPos = (setmov + stp3*.5);
walls[tally]=newWallPos;
break;
}
else
{
dwalls = GameObject.FindGameObjectsWithTag ("wall");
for (var dwall in dwalls)
{
if(dwall.transform.position == setmov + stp3*.5 ){Destroy(dwall);}
}
}
}
neighbours[m]=(setmov);
print(walls[tally]);
print(walls.length);
}
}