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());
}
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