Why does this while loop freeze unity?
Hello, I need help with a fix for my while loop as it crashes unity when I run it. This is what it looks like :
while (ifList.Count > 0) {
Debug.Log ("while");
}
It is contained within a coroutine so it should pause that coroutine until ifList contains nothing, right? I clear the ifList at a later stage from another coroutine.
Please help.
Co-routines are not black magic. They are "cooperative routines". If you refuse to cooperate (don't call any yield inside a piece of code) that code will run until it's finished. If a loop without a yield can't finish (which is the case in your example) you've just created an infinite loop --> game freezes,
Answer by AurimasBlazulionis · Sep 10, 2016 at 12:44 PM
No, it does not pause coroutine by any means. You are telling unity to simply spam debug.log in one frame infinitely. If you want to wait until ifList.Count is zero and check that only every fixed update, add yield return new WaitForFixedUpdate ();
after Debug.Log.
Your answer
Follow this Question
Related Questions
[Solved]Cant Find loop that freezes Unity 2 Answers
While loop freeze 0 Answers
Do-While Loop not working 1 Answer
While loop causing Unity to hang up? 1 Answer