Jump to content
Unity Insider Forum
Sign in to follow this  
Nyen Dev

Coroutine while(bool) kills Unity

Recommended Posts

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 

Share this post


Link to post
Share on other sites

You're missing the curly braces.

while (flag)
{
  DoSomething();

  yield return null; 
}

 

Share this post


Link to post
Share on other sites
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. 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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 

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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 

Share this post


Link to post
Share on other sites

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;

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...