Unity - IEnumerator's yield return null
Asked Answered
W

3

22

I'm currently trying to understand IEnumerator & Coroutine within the context of Unity and am not too confident on what the "yield return null" performs. At the moment i believe it basically pauses and waits for the next frame and in the next frame it'll go back to perform the while statement again.

If i leave out the "yield return null" it seems the object will instantly move to its destination or perhaps "skip a lot of frames". So i guess my question is how does this "yield return null" function within this while loop and why is it necessary to have it.

void Start () {
    StartCoroutine(Move());
}

IEnumerator Move(){

    while (a > 0.5f){

        ... (moves object up/down)

        yield return null; // <---------
    }

    yield return new WaitForSeconds(0.5f);

    .... (moves object up/down)

    StartCoroutine(Move());
}
Wicked answered 18/1, 2017 at 13:5 Comment(4)
I'm not familiar with Coroutines myself, but my guess is that they will run once per frame (or something) and the yield return is basically a shortcut for saying "wait until next update to continue". If you don't have it, the while loop will run all the way to completion on a single update, which is why the object moves instantly without it.Debidebilitate
@Abion47, so if there are say 40 iterations to make and we added yield return null inbetween, does it mean that it will need 40 frames to complete the iteration?Paba
and if so, the movement will then depend on the fps? if 60 fps then it will reach the target in less then a seconds and if 20fps, it will reach the target at 2 seconds?Paba
@Paba Basically, yeah.Debidebilitate
C
46

The program will start the loop, if you had no yield, it simply runs all iterations within the same frame. If you had millions of iterations, then it would most likely block your program until all iterations are done and then continue.

When creating a coroutine, Unity attaches it to a MonoBehaviour object. It will run first on call for the StartCoroutine until a yield is hit. Then it will return from the coroutine and place it onto a stack based on the yield. If you yield null, then it will run again next frame. There are a number of different YieldInstruction's that can be returned from a coroutine, you can read more about them here and through the related links.

Once a coroutine has yielded, the Main Thread continues running. On the next frame, Unity will find stacked coroutine and will call them from where they left off at the yield. If your coroutine never runs out of scope then you basically created an update method.

The purpose of coroutine is to perform actions that could span over a period of time without blocking the program.

IMPORTANT FACT: this is not multi-threading.

Conchiferous answered 18/1, 2017 at 13:16 Comment(2)
Another question. On the next loop, does Unity will run the yield coroutines first, wait for it to yield again or finish and then execute the update methods, or execute the update methods first?Boxcar
See the link, it shows coroutine is handled between update and animation. So it will run your update first and then move on to check if any pending coroutine.docs.unity3d.com/Manual/ExecutionOrder.htmlConchiferous
I
10

You are correct. yield return null will wait until the next frame and then continue execution. In your case it will check the condition of your while loop the next frame.

The "why this is necessary" is probably because you want the object to move by an input every frame. Without yield return null it just executes trough the while loop in one frame.

More essential: It looks like you want to Update every frame and adjust the psoition. You could easily use the Update () for that. This function will get called by Unity every frame on an active script.

Inclinable answered 18/1, 2017 at 13:13 Comment(0)
A
0

you have to return something in ienumorator, its not a void method. you can also yield return new WaitforSecond(1f)to wait for longer

Affix answered 27/12, 2022 at 0:25 Comment(2)
What new information does this answer add to a five year old question?Gynoecium
woop didnt notice the time, no need to hateAffix

© 2022 - 2024 — McMap. All rights reserved.