Question by
Tim-the-Blue-Mage · Apr 30, 2021 at 07:12 PM ·
arrays
Iterating over array problem
I'm trying to grab all the floor tiles in a scene and store them in an array. After I've done that, I reorganize them into a new array based on their positioning. This script seems to fall apart when we start to arrange them into the new array. Where is my mistake here?
GameObject[] initFloorTiles;
GameObject[] floorTiles;
void Awake()
{
initFloorTiles = GameObject.FindGameObjectsWithTag("floorTile");
}
// Start is called before the first frame update
void Start()
{
for(int i = 0; i < initFloorTiles.Length; i++)
{
GameObject currFloorTile = initFloorTiles[i];
switch (initFloorTiles[i].transform.position.z)
{
case 0:
switch (initFloorTiles[i].transform.position.x)
{
case 0:
floorTiles[0] = initFloorTiles[i];
Debug.Log("0");
break;
case 1:
floorTiles[1] = initFloorTiles[i];
Debug.Log("1");
break;
case 2:
floorTiles[2] = initFloorTiles[i];
Debug.Log("2");
break;
default:
break;
}
break;
case 1:
switch (initFloorTiles[i].transform.position.x)
{
case 0:
floorTiles[3] = initFloorTiles[i];
Debug.Log("3");
break;
case 1:
floorTiles[4] = initFloorTiles[i];
Debug.Log("4");
break;
case 2:
floorTiles[5] = initFloorTiles[i];
Debug.Log("5");
break;
default:
break;
}
break;
case 2:
switch (initFloorTiles[i].transform.position.x)
{
case 0:
floorTiles[6] = initFloorTiles[i];
Debug.Log("6");
break;
case 1:
floorTiles[7] = initFloorTiles[i];
Debug.Log("7");
break;
case 2:
floorTiles[8] = initFloorTiles[i];
Debug.Log("8");
break;
default:
break;
}
break;
case 3:
switch (initFloorTiles[i].transform.position.x)
{
case 0:
floorTiles[9] = initFloorTiles[i];
break;
case 1:
floorTiles[10] = initFloorTiles[i];
break;
case 2:
floorTiles[11] = initFloorTiles[i];
break;
default:
break;
}
break;
case 4:
switch (initFloorTiles[i].transform.position.x)
{
case 0:
floorTiles[12] = initFloorTiles[i];
break;
case 1:
floorTiles[13] = initFloorTiles[i];
break;
case 2:
floorTiles[14] = initFloorTiles[i];
break;
default:
break;
}
break;
case 5:
switch (initFloorTiles[i].transform.position.x)
{
case 0:
floorTiles[15] = initFloorTiles[i];
break;
case 1:
floorTiles[16] = initFloorTiles[i];
break;
case 2:
floorTiles[17] = initFloorTiles[i];
break;
default:
break;
}
break;
default:
break;
}
}
for(int i = 0; i < floorTiles.Length; i++)
{
Debug.Log("X position is " + floorTiles[i].gameObject.transform.position.x + " and Z position is " + floorTiles[i].gameObject.transform.position.z);
}
Comment
What exactly are you trying to achieve ? It seems like you're trying to go over all the floor tiles you have found by the Tag and then fill another array with them, but I'm not able to tell from your code what the actual goal of it is, as it tried to iterate over the tiles you found, but uses switch statements within switch statements which seem rather confusing.