Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity - IEnumerator's yield return null

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());
}
like image 632
WonderfulWonder Avatar asked Jan 18 '17 13:01

WonderfulWonder


People also ask

What does yield return null do Unity?

You use "yield return null;" when you want to skip a frame within that test. It just means, run the code below "yield return null;" next frame (not right now). For example, I want to do two tests. Check if a gameobject has Rigidbody component. Check if a gameobject is properly moving (ie: falling down)

Does yield break return null?

"yield break" breaks the Coroutine (it's similar as "return"). "yield return null" means that Unity will wait the next frame to finish the current scope. "yield return new" is similar to "yield return null" but this is used to call another coroutine.

What is yield return Unity?

The yield return statement is special; it is what actually tells Unity to pause the script and continue on the next frame. There are a number of ways that can be used to yield return; one of which is to create an instance of the WaitForSeconds class.

Can you yield return a coroutine?

The StartCoroutine method returns upon the first yield return, however you can yield the result, which waits until the coroutine has finished execution. There is no guarantee coroutines end in the same order they started, even if they finish in the same frame.


2 Answers

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.

like image 167
Everts Avatar answered Oct 07 '22 01:10

Everts


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.

like image 24
Max Auer Avatar answered Oct 07 '22 01:10

Max Auer