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

WaitForSeconds

Suspends the coroutine execution for the given amount of seconds.

WaitForSeconds can only be used with an yield statement in coroutines.

Example

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

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