Suggest a change Success!Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close Submission failedFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation. Close Your name Your email Suggestion* Cancel Switch to Manual Declarationpublic Coroutine StartCoroutine(IEnumerator routine); DescriptionStarts 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; Another example: // In this example we show how to invoke a coroutine and wait until it // is completed Declarationpublic Coroutine StartCoroutine(string methodName, object value = null); DescriptionStarts a coroutine named 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. 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; |