Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In Unity, we have some special things for coroutines that are additional to normal C#.

for example, we can use

yield return WaitForSeconds(5.f);

to have a coroutine wait 5 seconds before continuing.

What do yield return false; and yield return true; do?

share|improve this question
    
hm I'm not sure if yield return true does something special. But yield return null simply pauses for a frame, so I suspect that yield return false has the exact same effect (also yield return 0 since 0 false null are all the same under the hood) –  jhocking Aug 5 at 15:47
    
@jhocking “since 0 false null are all the same under the hood” Why does that matter? C# is very much a strongly-typed language, so most of the time, how something looks “under the hood” doesn't matter. For example, you can't just do if (0) or if (null), unlike in C and C++. –  svick Aug 5 at 18:32
    
shrug then it doesn't matter. all I know is that yield return 0 works; apparently because of the reason MLM explained –  jhocking Aug 5 at 20:23
1  
This is not additional to normal C#. The yield keyword has been around since at least C# 2.0. All Unity does is call it coroutine and handles the generated iterator and the values returned from it in a way that you can cause non-blocking delays in the execution of your code. –  O. R. Mapper Aug 6 at 6:20
    
If I were in a .NET C# project made with Visual Studio without Unity and I did yield return WaitForSeconds(5.f); it would not compile. I am aware that yield is not new. But you can do different things with it in Unity, and they are not clear at all in their documentation about what is special to Unity and what is not. –  Almo Aug 6 at 12:15

1 Answer 1

up vote 13 down vote accepted

The only possible yield values the scheduler understands are:

  • Classes derived from YieldInstruction (WaitForSeconds, WaitForEndOfFrame, WaitForFixedUpdate, AssetBundleCreateRequest, AssetBundleRequest and Coroutine
  • a WWW object
  • "any other value" which isn't one of the above.

If "any other value" is yielded (which includes "null" a string value any other basic type such as int, bool, ... or a reference to an arbitrary object which isn't one of the above mentioned) the scheduler will "schedule" the coroutine for the next frame.

- Bunny83 answer: Prominent member on Unity Answers

The WaitForEndOfFrame and others of the like, are just blank functions that tag the YieldInstruction in order to decide what to do in the engine.

The default case seems to be WaitForEndOfFrame. So if you yield return something that doesn't have a special meaning, such as a bool, it is the same as WaitForEndOfFrame.

There doesn't seem to be any official documentation on this behavior.


Update

rutter commented about another special case: yield return null

All of the Unity Coroutines including yield return null, run before the frame renders except for WaitForEndOfFrame. You can find rutter's awesome answer over at Unity Answers explaining this further (nice diagrams included).

share|improve this answer
3  
It's common to assume that yielding null and WaitForEndOfFrame are the same, but they are actually slightly different, as detailed at the execution order manual page. –  rutter Aug 6 at 0:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.