Version: 2017.4
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

MonoBehaviour.StartCoroutine

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 failed

For 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

Cancel

public method StartCoroutine(routine: IEnumerator): Coroutine;
public Coroutine StartCoroutine(IEnumerator routine);

Description

Starts a coroutine.

The execution of a coroutine can be paused at any point using the yield statement. The yield return value specifies when the coroutine is resumed. Coroutines are excellent when modelling behaviour over several frames. Coroutines have virtually no performance overhead. StartCoroutine function always returns immediately, however you can yield the result. This will wait until the coroutine has finished execution. There is no guarantee that coroutines end in the same order that they were started, even if they finish in the same frame.

When using JavaScript it is not necessary to use StartCoroutine, the compiler will do this for you. When writing C# code you must call StartCoroutine.

#pragma strict
// In this example we show how to invoke a coroutine and continue executing
// the function in parallel.
private var coroutine: IEnumerator;
function Start() {
	// - 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 function WaitAndPrint(waitTime: float) {
	while ( true ) {
		new WaitForSeconds(waitTime)print("WaitAndPrint " + Time.time);
	}
}
using UnityEngine;
using System.Collections;

// In this example we show how to invoke a coroutine and continue executing // the function in parallel.

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:

#pragma strict
// In this example we show how to invoke a coroutine and wait until it
// is completed
function Start() {
	// - After 2 seconds, prints "Done 2.0"
	print("Starting " + Time.time);
	StartCoroutine(WaitAndPrint(2.0F))print("Done " + Time.time);
}
// suspend execution for waitTime seconds
function WaitAndPrint(waitTime: float) {
	new WaitForSeconds(waitTime)print("WaitAndPrint " + Time.time);
}
// 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 WaitAndPrint(2.0); 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); } }

public method StartCoroutine(methodName: string, value: object = null): Coroutine;
public Coroutine StartCoroutine(string methodName, object value = null);

Description

Starts a coroutine named methodName.

In most cases you want to use the StartCoroutine variation above. 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 runtime overhead to start the coroutine and you can pass only one parameter.

#pragma strict
// In this example we show how to invoke a coroutine using a string name and stop it.
function Start() {
	StartCoroutine("DoSomething", 2.0F);
	new WaitForSeconds(1)StopCoroutine("DoSomething");
}
function DoSomething(someParameter: float) {
	while ( true ) {
		print("DoSomething Loop");
		null}
}
// 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; } } }

Parameters

coroutineName of the created Coroutine.

Description

Starts a Coroutine named coroutine.

Create a Coroutine. Any coroutine name can be used. A StartCoroutine function terminates immediately, however, the Coroutine it creates runs as expected. A created coroutine can start another coroutine. These two coroutines can operate together in many ways. This includes both coroutine running in parallel. Alternatively one coroutine can stop the other while it continues itself. The script example below shows one coroutine pausing as it starts another one. Once this second coroutine finishes it restarts the first one.

#pragma strict
function Start() {
	StartCoroutine(coroutineA());
}
function coroutineA() {
	// wait for 1 second
	Debug.Log("coroutineA created");
	new WaitForSeconds(1.0f)StartCoroutine(coroutineB())Debug.Log("coroutineA running again");
}
function coroutineB() {
	Debug.Log("coroutineB created");
	new WaitForSeconds(2.5f)Debug.Log("coroutineB enables coroutineA to run");
}
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"); } }