- Home /
Looping through multiple lists
Hey i have this code where i loop through my list of gameobjects and my list of positions where they should be instantiated. I have 8 gameobjects in a gameobjec list and i have 8 positions in a transform list. The code doesnt work as it should, and it breaks the game because argument is out of range.
Here it is:
if(rest == length){
spawnRow2.Add(piece.transform);
}
while(startPos <= 7)
{
for(int i = 0; i <bPieces.Length; i++){
Instantiate(bPieces[i + startPos], spawnRow2[i + startPos].position, Quaternion.identity);
startPos++;
}
}
bPieces is the gameobject list. spawnRow is the transform list. startPos is a int at 0. What i do in the first if-statement is to grab a row of positions and put it in the transform list and it works fine.
But in the loop below it breaks, its supposed to spawn one object at one position, then it will shuffle to the next object and next position. Once it reaches the end of both the lists, the loop should stop.
But yeah i get the error when running: "ArgumentOutOfRangeException: Argument is out of range."
Thanks