I'm new and a bit confused about "yield
". But finally I understand how it worked using WaitForSeconds
but I can't see the difference between of "yield return 0
" and "yield return null
".
are both them waiting for the next frame to execute?
sorry for my bad English. Thank you very much.
Yielding of any type, including null, results in the execution coming back on a later frame, unless the coroutine is stopped or has completed. Note: You can stop a coroutine using MonoBehaviour.
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).
1. "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.
The yield return statement is special; it is what actually tells Unity to pause the script and continue on the next frame.
Both yield return 0
and yield return null
yields for a single frame. The biggest difference is that yield return 0
allocates memory because of boxing and unboxing of the 0
that happens under the hood, but yield return null
does not allocate memory. Because of this, it is highly recommended to use yield return null
if you care about performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With