Unity - IEnumerator output returns null

I am currently trying to understand IEnumerator and Coroutine in the context of Unity and am not too sure that it is performing a "yield return null". At the moment, I believe that it basically pauses and waits for the next frame, and in the next frame it will return to execute the while statement again.

If I omit the "yield return null", it seems that the object will instantly move to the destination, or perhaps "skip a lot of frames." So, I think, my question is how is this "yield return null" function inside this while loop and why is it needed.

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());
}
+6
source share
2

, , . , , , , ,

Unity MonoBehaviour. StartCoroutine, . . , . YieldInstruction, , .

, . Unity , . , .

, , , .

: .

+17

. yield return null , . while .

" ", , , , . yield return null while .

: , Update psoition. Update (). Unity script.

+4

Source: https://habr.com/ru/post/1667140/


All Articles