17
\$\begingroup\$

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?

\$\endgroup\$
9
  • \$\begingroup\$ 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) \$\endgroup\$
    – jhocking
    Commented Aug 5, 2014 at 15:47
  • \$\begingroup\$ @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++. \$\endgroup\$
    – svick
    Commented Aug 5, 2014 at 18:32
  • \$\begingroup\$ shrug then it doesn't matter. all I know is that yield return 0 works; apparently because of the reason MLM explained \$\endgroup\$
    – jhocking
    Commented Aug 5, 2014 at 20:23
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 6, 2014 at 6:20
  • 1
    \$\begingroup\$ @Almo: That is simply because WaitForSeconds is not declared. As soon as you declare a method called WaitForSeconds that takes a float argument and that returns the element type of the enumeration you are implementing, it would compile again. Hence, yield is not special to Unity. If anything, the WaitForSeconds method is special to Unity, but even then, it does not extend the language C#, but is simply an extra method that was written using standard C#. \$\endgroup\$ Commented Aug 12, 2014 at 13:39

1 Answer 1

19
\$\begingroup\$

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

\$\endgroup\$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .