Version: 5.4 beta (switch to 5.3)
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

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public function StopCoroutine(methodName: string): void;
public void StopCoroutine(string methodName);
public function StopCoroutine(routine: IEnumerator): void;
public void StopCoroutine(IEnumerator routine);

Parameters

methodName Name of coroutine.
routine Name of the function in code.

Description

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

	// In this example we show how to invoke a coroutine using a string name and stop it

function Start () { StartCoroutine("DoSomething", 2.0); yield WaitForSeconds(1); StopCoroutine("DoSomething"); }

function DoSomething (someParameter : float) { while (true) { print("DoSomething Loop"); // Yield execution of this coroutine and return to the main loop until next frame yield; } }
using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {

private IEnumerator coroutine;

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

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); } } }