O'Reilly logo

Unity 3 Game Development HOTSHOT by Jate Wittayabundit

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

YieldInstruction and coroutine

There are differences in the syntax of C# and JavaScript as follows:

JavaScript:

yield WaitForSeconds(3);          //pauses for 3 seconds
yield WaitForMyFunction();          //start coroutine 

function WaitForMyFunction() {...}      //coroutine function

C#:

yield return new WaitForSeconds(3);    //pauses for 3 seconds
yield return WaitForMyFunction();    //start coroutine

IEnumerator WaitForMyFunction() {...}    //coroutine function

Tip

In JavaScript, it will automatically generate the return type to IEnumerator if you put yield instruction inside the function. On the other hand, in C# you will need to specify the return type to IEnumerator.

However, if we want to wait for the user input in C#, which might be over several frames, we will have to use ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required