Legacy Documentation: Version 5.2
LanguageEnglish
  • C#
  • JS

Script language

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

MonoBehaviour.StopCoroutine

Switch to Manual
public void StopCoroutine(string methodName);
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.

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