Suspends the coroutine
execution for the given amount of seconds.
WaitForSeconds
can only be used with an yield
statement in coroutines
.
function Start() { // Prints 0 Debug.Log (Time.time); // Waits 5 seconds yield WaitForSeconds (5); // Prints 5.0 Debug.Log (Time.time); }
You can both stack and chain coroutines
.
The following example will execute Do
but will continue after calling Do
immediately:
function Start() { Do(); Debug.Log ("This is printed immediately"); } function Do() { Debug.Log ("Do now"); yield WaitForSeconds (5); //Wait for 5 seconds Debug.Log ("Do 5 seconds later"); }
The following example will execute Do
and wait
until it is finished before continuing its own execution:
//Chain Coroutine function Start() { //The below line is similar to theyield Do();
only if you are using Unity JavaScript. However, if you use C#, you must useStartCoroutine
. (For more details in the Appendix C) yield StartCoroutine(Do()); Debug.Log ("This is printed after 5 seconds"); Debug.Log ("This is after theDo
coroutine has finished execution"); } function Do() { Debug.Log ("Do now"); yield WaitForSeconds (5); //Wait for 5 seconds Debug.Log ("Do 5 seconds later"); }
3.12.136.119