- Home /
Loop not executing all code - for in loop
Hi Everyone,
I have a for in loop that doesn't work the same each iteration, code is below. Code is pretty simple, it gets a list of GameObjects, and then calls 1 function as it works through each item in the list of max 12.
What happens instead is very odd.
The 1st Loop everything seems to work.
The 2nd Loop the calculateMoves() function gets called - BUT the print statements afterwards don't fire. BUT the indexNum = indexNum + 1; does
The 3rd --> Last loop, just the Print ".name" part at the top of the loop & the index incrementing happen, it appears like the function call never happens & the print "Done" statement below the function don't execute.
I can't see how it's executing just 3 out of 6 lines in the code on some loops, when it executed all 6 the first go.
This code is NOT within the Update() path so should be a straight execution.
I'm a bit mystified as to what is going on.
- The calculateMoves() is being called as some type of co-routine and the loop isn't waiting for it to return ?
- Perhaps I have some mistake with the scope of the local/public variables ? as I am mixing them, bad form but I'm making calculations to build a global grid.
function getRedTeamMoves() : int {
var RedTeamList = GameObject.FindGameObjectsWithTag("RedTeam");
var statusTM : int;
indexNum = 0;
for (RedTeamMember in RedTeamList)
{
print ("RedTeamMember.name = " + RedTeamMember.name);
statusTM = calculateMoves(RedTeamMember);
print ("****** Calculations Done for this bot *************");
print ("*******************");
indexNum = indexNum + 1;
print ("indexNum = " +indexNum);
}
return 1;
}
Any help appreciated, hopefully it's something really silly.
Answer by Eric5h5 · Aug 25, 2012 at 05:48 PM
Turn off "collapse" in the console.
Thank you! That's bitten me before when debugging. Collapse was masking a bug in my code where the function kept calculating the same values every time even when the inputs were different.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080154im_/https://answers.unity.com/themes/thub/images/avi.jpg)