Game Development Reference
In-Depth Information
WaitForSeconds
WaitForSeconds
suspends the
coroutine
execution for the given amount of
seconds.
WaitForSeconds
can only be used with a
yield
statement in
coroutines
. An ex-
ample of
WaitForSeconds
is as follows:
// JavaScript user:
function Start() {
// Prints 0
Debug.Log (Time.time);
// Waits 5 seconds
yield WaitForSeconds (5);
// Prints 5.0
Debug.Log (Time.time);
}
// C# user:
IEnumerator Start() {
// Prints 0
Debug.Log (Time.time);
// Waits 5 seconds
yield return new WaitForSeconds (5f);
// Prints 5.0
Debug.Log (Time.time);
}
You can both stack and chain
coroutines
.
The following example will execute
Do()
. We will see that it will call the first line in
Do()
. Then, it will call the last line in
Start()
immediately, while waiting for
5
seconds
to call the last script in
Do()
.