Starts a Coroutine.
The execution of a coroutine can be paused at any point using the yield statement. When a yield statement is used, the coroutine pauses execution and automatically resumes at the next frame. See the Coroutines documentation for more details.
Coroutines are excellent when modeling behavior over several frames. The StartCoroutine method returns upon the first yield return, however you can yield the result, which waits until the coroutine has finished execution. There is no guarantee coroutines end in the same order they started, even if they finish in the same frame.
Yielding of any type, including null, results in the execution coming back on a later frame, unless the coroutine is stopped or has completed.
Note: You can stop a coroutine using MonoBehaviour.StopCoroutine and MonoBehaviour.StopAllCoroutines. Coroutines are also stopped when the MonoBehaviour is destroyed or if the GameObject the MonoBehaviour is attached to is disabled.
Coroutines are not stopped when a MonoBehaviour is disabled.
See also: Coroutine, YieldInstruction
An example of StartCoroutine:
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { // In this example we show how to invoke a coroutine and // continue executing the function in parallel.
private IEnumerator coroutine;
void Start() { // - After 0 seconds, prints "Starting 0.0" // - After 0 seconds, prints "Before WaitAndPrint Finishes 0.0" // - After 2 seconds, prints "WaitAndPrint 2.0" print("Starting " + Time.time);
// Start function WaitAndPrint as a coroutine.
coroutine = WaitAndPrint(2.0f); StartCoroutine(coroutine);
print("Before WaitAndPrint Finishes " + Time.time); }
// every 2 seconds perform the print() private IEnumerator WaitAndPrint(float waitTime) { while (true) { yield return new WaitForSeconds(waitTime); print("WaitAndPrint " + Time.time); } } }
Another example:
// In this example we show how to invoke a coroutine and wait until it // is completed
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { IEnumerator Start() { // - After 0 seconds, prints "Starting 0.0" // - After 2 seconds, prints "WaitAndPrint 2.0" // - After 2 seconds, prints "Done 2.0" print("Starting " + Time.time);
// Start function WaitAndPrint as a coroutine. And wait until it is completed. // the same as yield return WaitAndPrint(2.0f); yield return StartCoroutine(WaitAndPrint(2.0f)); print("Done " + Time.time); }
// suspend execution for waitTime seconds IEnumerator WaitAndPrint(float waitTime) { yield return new WaitForSeconds(waitTime); print("WaitAndPrint " + Time.time); } }
Starts a coroutine named methodName
.
In most cases you want to use the StartCoroutine variation above. However StartCoroutine using a string method name lets you use StopCoroutine with a specific method name. The downside is that the string version has a higher runtime overhead to start the coroutine and you can pass only one parameter.
// In this example we show how to invoke a coroutine using a string name and stop it.
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { IEnumerator Start() { StartCoroutine("DoSomething", 2.0f); yield return new WaitForSeconds(1); StopCoroutine("DoSomething"); }
IEnumerator DoSomething(float someParameter) { while (true) { print("DoSomething Loop");
// Yield execution of this coroutine and return to the main loop until next frame yield return null; } } }
A created coroutine can start another coroutine. These two coroutines can operate together in many ways. This includes both coroutines running in parallel. Alternatively one coroutine can stop the other while it continues itself. The example below shows one coroutine pausing as it starts another one. When the second coroutine finishes it restarts the first one.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ExampleClass : MonoBehaviour { void Start() { StartCoroutine(coroutineA()); }
IEnumerator coroutineA() { // wait for 1 second Debug.Log("coroutineA created"); yield return new WaitForSeconds(1.0f); yield return StartCoroutine(coroutineB()); Debug.Log("coroutineA running again"); }
IEnumerator coroutineB() { Debug.Log("coroutineB created"); yield return new WaitForSeconds(2.5f); Debug.Log("coroutineB enables coroutineA to run"); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.