Nyen Dev Geschrieben 8. Oktober 2019 Melden Share Geschrieben 8. Oktober 2019 Why does it put Unity into a endless loop? I am relocating some code from Update into Coroutines, because I think it is just cleaner and resource wise better that way (using if in Update isn't needed most of the time and can be avoided using events and coroutines imo). It puts Unity instantly as the Coroutine is started into an endless-loop state Code is pretty simple while (flag) DoSomething() yield return null; and somewhere i would set the flag to false Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Sascha Geschrieben 8. Oktober 2019 Melden Share Geschrieben 8. Oktober 2019 You're missing the curly braces. while (flag) { DoSomething(); yield return null; } Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Nyen Dev Geschrieben 8. Oktober 2019 Autor Melden Share Geschrieben 8. Oktober 2019 10 minutes ago, Sascha said: You're missing the curly braces. while (flag) { DoSomething() yield return null; } But that would execute the code just once? I want it to keep on doing doSomething() until the flag is changed from somewhere else. Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Sascha Geschrieben 8. Oktober 2019 Melden Share Geschrieben 8. Oktober 2019 Doing something conditionally only once is done with an if statement. A while statement always means a loop. The braces only define what is being executed conditionally or multiple times. Omitting the braces means just including the next statement in the if or while statement, so your original code is equivalent to while (flag) { DoSomething() } yield return null; Which means you're infinitely executing DoSomething(), since the waiting part (yield return null) is never reached. Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Nyen Dev Geschrieben 8. Oktober 2019 Autor Melden Share Geschrieben 8. Oktober 2019 Just now, Sascha said: Doing something conditionally only once is done with an if statement. A while statement always means a loop. The braces only define what is being executed conditionally or multiple times. Omitting the braces means just including the next statement in the if or while statement, so your original code is equivalent to while (flag) { DoSomething() } yield return null; Which means you're infinitely executing DoSomething(), since the waiting part (yield return null) is never reached. I know it is the same, just keeps it short. And I do want it to repeat. The flag is changed somewhere else in the code when other conditions are met, and they are for sure. But starting this coroutine instantly kills unity Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Sascha Geschrieben 8. Oktober 2019 Melden Share Geschrieben 8. Oktober 2019 Please re-read my post. Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Nyen Dev Geschrieben 8. Oktober 2019 Autor Melden Share Geschrieben 8. Oktober 2019 I am pretty confused now, wasnt yield return null to terminate the coroutine? Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Sascha Geschrieben 8. Oktober 2019 Melden Share Geschrieben 8. Oktober 2019 No, it's to interrupt the coroutine, and to continue it in the next frame. However, the line is never reached in the first place, as you're infinitely repeating DoSomething() without that pause (in which the flag could be set to false). Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Nyen Dev Geschrieben 8. Oktober 2019 Autor Melden Share Geschrieben 8. Oktober 2019 Well thank you, I misunderstood the usage of yield return null then. Thank you very much. Since I am on this already what do you write to end the coroutine then? Since you have to yield something Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Sascha Geschrieben 8. Oktober 2019 Melden Share Geschrieben 8. Oktober 2019 If the program reaches the end of the method, the coroutine ends, but if you need to terminate the coroutine before that point, your friend is yield break; Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Recommended Posts
Archiviert
Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.