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 below line is similar to the yield Do(); only if you are using Unity JavaScript. However, if you use C#, you must use StartCoroutine. (For more details in the Appendix C)
  yield StartCoroutine(Do());

  Debug.Log ("This is printed after 5 seconds");
  Debug.Log ("This is after the Do coroutine has finished execution");
}

function Do() {
  Debug.Log ("Do now");
  yield WaitForSeconds (5); //Wait for 5 seconds
  Debug.Log ("Do 5 seconds later");
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.77.153