- Home /
For loop getting skipped/doesnt work
Here's the scenario: I've got a simple for loop in a coroutine that was working perfectly, and then suddenly stopped evaluating.
Here's what is looks like, with some debug messages I added in:
public static IEnumerator CamGoHome () {
// do stuff
yield return new WaitForSeconds (flyTime);
Debug.Log ("hi1");
for (int i = 20; i <= 0; i--) {
Debug.Log ("hi2");
grey.renderer.enabled = !grey.renderer.enabled;
// yield return new WaitForSeconds (0.5f * i);
}
Debug.Log ("hi3");
// do other stuff
}
So, this worked before: the renderer of grey blinked, and this worked in multiple tests. I fiddled with the yield return timing in the for loop, saved, and ... the for loop suddenly stopped evaluating.
I've changed the int used in the for loop, the parameters, cleared the loop out completely. I've deleted it and rewritten it. The coroutine always completes (I see the "hi1" and "hi3" messages in the console every time) but it seems to just completely ignore the for loop, or any new for loop of any kind written into the same section of the routine. If I write a for loop up above, it does work, but in that particular section of code it's just invisible.
Anyone know what would cause this?
// yield return new WaitForSeconds (0.5f * i);
uncommment this line....
Actually it is working now also but everything happens in one frame, so you cant see the change.
Sorry, not relevant - I just had it commented out at that particular moment. The debug messages would appear in the console regardless.
Answer by flamy · Apr 03, 2014 at 11:07 AM
for (int i = 20; i <= 0; i--)
this line is the problem, especially the condition. you are checking if i is less than or equal to 0 at the begining obviously 20
try
for (int i = 20; i >= 0; i--) //for 21 times
for (int i = 20; i > 0; i--) //for 20 times
it is always better to keep the counts increasing rather than decreasing, unless you have the absolute need to do so!
for (int i = 0; i < 20; i++) //for 20 times
I have to apologize flamy - you're being very helpful but I really screwed up the code I pasted in. I was getting frustrated and just typing in various values. $$anonymous$$y starting values for the code were actually the same as what you suggested, and that's what originally stopped working.
So, it really boggled me when I pasted in your answer and... it worked ;) However, it still had a really weird problem - ins$$anonymous$$d of evaluating 20 times, it evaluated 3-4 times.
The problem appears to be in the yield returns. I don't have a particularly good understanding of what's going on, but it looks like yields that are too short cause the for loop to end - maybe because the time fraction is shorter than a frame? Anyway, that would explain why my initial change to the yield is the point at which the for loop stopped evaluating.
I'm going to poke at it some more then post an answer, I guess. Weird problem!
And.. I never figured this out. It all now works as if nothing ever happened. I'm going to mark your answer as correct just in case I was having a massive, long-running brain fart and repeatedly messed up the for loops (although I'd swear the moment it stopped working is when I changed the yield ti$$anonymous$$g...)
Your answer
![](https://koobas.hobune.stream/wayback/20220613141835im_/https://answers.unity.com/themes/thub/images/avi.jpg)