- Home /
Yield return null?
Please someone explain to me what is "yield return null" in coroutines?
For example if we have this code..
void update{
StartCoroutine(MoveEnemy());
}
IEnumerator MoveEnemy(){
While(true){
somecode...
yield return null; // Whats the use of this line?
}
}
I know it means run the code until the next frame, but what does that even mean?
Thank you.
@rashiddev since your question has already been well answered, I'll just add as a side note that it's bad practice to start a coroutine inside Update, especially if it's done unconditionally. It always leads to problems. For example, in the code you posted, a new instance of the $$anonymous$$oveEnemy() coroutine will be started in every single frame. So at 60FPS, 600 $$anonymous$$oveEnemy() instances will have been created after 10 seconds of the Update execution, each running the while() loop. Not something that you'd want, or do intentionally.
Answer by Bunny83 · Dec 18, 2018 at 04:03 AM
The yield keyword is not a Unity feature. It's a C# feature called "iterator block". It allows to easily write iterator objects. The C# compiler actually turns your coroutine method into a compiler generated state machine class.
Unity uses those to implement coroutines. They are actually meant for things like this:
IEnumerable<int> SomeIterator()
{
yield return 0;
yield return 42;
yield return 7;
for (int i = 0; i < 10; i++)
yield return i*2;
}
This is also called a "generator method". When you call this method you actually create an instance of the internally generated statemachine class which implements the IEnumerable interface. This can be used for example in a foreach loop:
foreach(int val in SomeIterator())
{
Debug.Log(val);
}
This will print those 13 values: 0, 42, 7, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18. In Unity's coroutines Unity uses the value you return to determine when to continue / iterate your statemachine. If you return the value null (or any other value Unity doesn't recognise) Unity will simple re-schedule the coroutine the next frame. However if you return an instance of the WaitForSeconds class, Unity will wait for the specified time until it continues your coroutine.
This great blog article (which unfortunately only exists in the web archieve) explains this quite well with a simple example
The yield keyword is not Unity feature. But StartCoroutine() is Unity feature. StartCoroutine() function works with iterator in it's own way.
Answer by Andrey-Postelzhuk · Dec 18, 2018 at 09:56 AM
Short answer:
yield return null - wait for the next frame and continue execution from this line
It does not wait for end of frame, that is WaiForEndOfFrame, yield return null is updated many time in single frame.
yield return null is updated many time in single frame
That's not what the diagram you provided shows. The control of the coroutine yielding null is returned after each call to all the Update
functions, which are called once per frame.
I wrote "wait for the NEXT frame". This is not the same as "end of the frame". And "yield return null" definitely called only once per frame. Read your own answer again.
And in a while(true) loop inside a coroutine, that means the program (game) breaks out of the endless while loop to do everything outside the while loop until the next frame update, then continues the while loop from that line.
So, you need the yield return null in the while(true) loop because without it, a while(true) is an infinite loop that would in theory crash your game. At least that's my understanding.
Answer by darkStar27 · Dec 18, 2018 at 04:20 AM
Unity performs many operations every frame:
Initialization
Physics Updates
Input processing
Game Updates (Update())
Rendering Graphics, GUI etc.
yield return null runs earliest of any other yield returns.
For eg: yield return new WaitForEndOfFrame() will work after yield return null.
Explaination:
When you use iterations (IEnumerator) without returns, Unity runs it countless amount of time in one frame, blocking the editor. Therefore, we have to use yield return to return the control back to unity as if it gains control back even for a split second the editor will not block.
You can know more about unity execution order from given flowchart:
Your answer
![](https://koobas.hobune.stream/wayback/20220612184206im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Example of co-routine for documentation 1 Answer
Trouble understanding coroutines 3 Answers
Can't get while loop to execute more than once in a coroutine before yielding [Solved] 2 Answers
Coroutine stopping before it is complete 1 Answer
Need opinions, or facts, about how to best go about programming this behavior. Basically Redstone. 0 Answers