- Home /
Code not executing when condition is true
I'm trying to organize some transforms in hierarchy by code. I have this function:
private GameObject[,] SpawnCullingGroups(GameObject[,] cG)
{
for(int i = 0; i < cG.GetLength(0); i++)
{
for (int j = 0; j < cG.GetLength(1); j++)
{
GameObject c = Instantiate(Culler, new Vector3(i * 32f,0,j * 32f), Quaternion.identity) as GameObject;
c.name = "CoullingGroup";
cG[i,j] = c;
foreach (Transform child in GameObject.Find("MapContainer").transform)
{
if (child.position.x >= c.transform.position.x && child.position.x <= (c.transform.position.x + 32)
&& child.position.z >= c.transform.position.z && child.position.z <= c.transform.position.z + 32) {
child.parent = cG [i, j].transform;
}
}
}
}
return cG;
}
The problem comes when this piece of code organizes just some of the transforms when they all do accomplish the condition. I get this: http://i.imgur.com/z7qvsPr.png
Extra info: All transforms have 3.2 X size and 3.2 Z size and are perfectly aligned.
I think your problem is the use of "less/greater or equal". The way you have it both cg[i,j] and cg[i+1,j+1] will be able to become the parent. If i=0 then cg[0,0] have the position 0,0. cg[1,1] have position 32,32. cg[2,2] have 64,64. so first the parent become cg[0,0] at position 0,0 for a child at position 32,32. That child will then get cg[1,1] as parent (it is equal to cg[1,1] position and less than [cg1,1]+32), since both statements are still true.
Your answer