Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "yield return 0" and "yield return null" in Coroutine?

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.

like image 233
yumugee Avatar asked Sep 01 '16 10:09

yumugee


People also ask

What does yield return null do in a coroutine?

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.

What does yield return null?

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).

Does yield break return null?

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.

What does yield return do Unity?

The yield return statement is special; it is what actually tells Unity to pause the script and continue on the next frame.


1 Answers

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.

like image 94
Programmer Avatar answered Oct 03 '22 15:10

Programmer