Game Development Reference
In-Depth Information
The following example will wait unil the WaitAndPrint funcion is inished and then
coninues execuing the rest of the code in the Start funcion:
function Start() {
// Starting = 0.0
Debug.Log ("Starting = " + Time.time);
// StartCoroutine WaitAndPrint (In JavaScript, you can also use
yield WaitAndPrint(5.0) which will get the same result.
yield StartCoroutine(WaitAndPrint(5.0));
// Done WaitAndPrint = 5.0
Debug.Log ("Done WaitAndPrint = " + Time.time);
}
function WaitAndPrint(waitTime : float) {
//Suspend execution for 5 seconds
yield WaitForSeconds(waitTime);
// WaitAndPrint = 5.0
Debug.Log ("WaitAndPrint = " + Time.time);
}
Using StartCoroutine with method name
(string)
In most cases, you would want to use the preceding StartCoroutine variaion. However,
StartCoroutine using a string method name allows you to use StopCoroutine with a
specific method name.
The downside is that the string version has a higher runime overhead
to start the corouine and you can pass only one parameter.
In the following example, we show how to invoke a corouine using a string name and how
to stop it:
function Start() {
// Start Coroutine DoSomething
StartCoroutine("DoSomething", 5.0);
// Wait for 2 seconds
yield WaitForSeconds(2.0);
 
Search WWH ::




Custom Search