Version: 2017.4
LanguageEnglish
  • C#
  • JS

Script language

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

MonoBehaviour.StopCoroutine

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 StopCoroutine(methodName: string): void;
public void StopCoroutine(string methodName);
public method StopCoroutine(routine: IEnumerator): void;
public void StopCoroutine(IEnumerator routine);
public method StopCoroutine(routine: Coroutine): void;
public void StopCoroutine(Coroutine routine);

Parameters

methodNameName of coroutine.
routineName of the function in code, including coroutines.

Description

Stops the first coroutine named methodName, or the coroutine stored in routine running on this behaviour.

StopCoroutine takes one of three arguments which specify which coroutine is stopped:

- A string function naming the active coroutine
- The IEnumerator variable used earlier to create the coroutine.
- The Coroutine to stop the manually created Coroutine.

Note: Do not mix the three arguments. If a string is used as the argument in StartCoroutine, use the string in StopCoroutine. Similarly, use the IEnumerator in both StartCoroutine and StopCoroutine. Finally, use StopCoroutine with the Coroutine used for creation.

In the CS example, the IEnumerator type is used.

#pragma strict
public class Example extends MonoBehaviour {
	private var coroutine: IEnumerator;
	// Use this for initialization
	function Start() {
		print("Starting " + Time.time);
		coroutine = WaitAndPrint(3.0f);
		StartCoroutine(coroutine);
		print("Done " + Time.time);
	}
	// yield is causing WaitAndPrint to pause every 3 seconds
	public function WaitAndPrint(waitTime: float) {
		while ( true ) {
			new WaitForSeconds(waitTime)print("WaitAndPrint " + Time.time);
		}
	}
	function Update() {
		if (Input.GetKeyDown("space")) {
			StopCoroutine(coroutine);
			print("Stopped " + Time.time);
		}
	}
}
using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour { // keep a copy of the executing script private IEnumerator coroutine;

// Use this for initialization void Start() { print("Starting " + Time.time); coroutine = WaitAndPrint(3.0f); StartCoroutine(coroutine); print("Done " + Time.time); }

// print to the console every 3 seconds. // yield is causing WaitAndPrint to pause every 3 seconds public IEnumerator WaitAndPrint(float waitTime) { while (true) { yield return new WaitForSeconds(waitTime); print("WaitAndPrint " + Time.time); } }

void Update() { if (Input.GetKeyDown("space")) { StopCoroutine(coroutine); print("Stopped " + Time.time); } } }

The following cs example shows how StopCoroutine(Coroutine) can be used.

#pragma strict
function Start() {
	StartCoroutine(coroutineA());
}
function coroutineA() {
	new WaitForSeconds(1.0f)Debug.Log("coroutineA() started: " + Time.time);
	new WaitForSeconds(1.0f)var b: Coroutine = StartCoroutine(coroutineB());
	new WaitForSeconds(2.0f)Debug.Log("coroutineA() finished " + Time.time);
	// but was shut down here after 3.0f
	StopCoroutine(b);
	null}
function coroutineB() {
	var f: float = 0.0f;
	var start: float = Time.time;
	Debug.Log("coroutineB() started " + start);
	while ( f < 10.0f ) {
		Debug.Log("coroutineB(): " + f);
		new WaitForSeconds(1.0f)f = f + 1.0f;
	}
	// coroutineA() shuts this down too early
	var t: float = Time.time - start;
	Debug.Log("coroutineB() finished " + t);
	null}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExampleClass : MonoBehaviour { void Start() { StartCoroutine(coroutineA()); }

IEnumerator coroutineA() { // wait for 1 second yield return new WaitForSeconds(1.0f); Debug.Log("coroutineA() started: " + Time.time);

// wait for another 1 second and then create b yield return new WaitForSeconds(1.0f); Coroutine b = StartCoroutine(coroutineB());

yield return new WaitForSeconds(2.0f); Debug.Log("coroutineA() finished " + Time.time);

// B() was expected to run for 10 seconds // but was shut down here after 3.0f StopCoroutine(b); yield return null; }

IEnumerator coroutineB() { float f = 0.0f; float start = Time.time;

Debug.Log("coroutineB() started " + start);

while (f < 10.0f) { Debug.Log("coroutineB(): " + f); yield return new WaitForSeconds(1.0f); f = f + 1.0f; }

// handling exit of the coroutine // coroutineA() shuts this down too early float t = Time.time - start; Debug.Log("coroutineB() finished " + t); yield return null; } }