# StopCoroutine

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

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.5/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

## StopCoroutine(IEnumerator)

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

```csharp
public void StopCoroutine(IEnumerator routine)
```

### Parameters

**** (\[IEnumerator]\(https\://learn.microsoft.com/dotnet/api/system.collections.ienumerator)): Name of the function in code, including coroutines.

### Remarks

`StopCoroutine` takes one of three arguments that specify which coroutine to stop:

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

You must use the same parameter type to stop a coroutine with `StopCoroutine` as was used to start it with [MonoBehaviour.StartCoroutine](/engine/6000.5/script-reference/unityengine/monobehaviour/startcoroutine.md).

Coroutines also stop if:

* The value of [GameObject.activeSelf](/engine/6000.5/script-reference/unityengine/gameobject/activeself.md) becomes `false` for the GameObject the script is attached to.
* The MonoBehaviour script is destroyed with a call to [Object.Destroy](/engine/6000.5/script-reference/unityengine/object/destroy.md).

**Note**: Disabling the MonoBehaviour script by setting [Behaviour.enabled](/engine/6000.5/script-reference/unityengine/behaviour/enabled.md) to `false` doesn't stop coroutines.

`StopCoroutine(null)` throws a NullReferenceException. Add checks to your code to ensure the argument is not null before calling `StopCoroutine`.

The following example uses the [IEnumerator](https://msdn.microsoft.com/en-us/library/system.collections.ienumerator\(v=vs.110\).aspx) to stop a coroutine.

```csharp
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 example uses the [Coroutine](/engine/6000.5/script-reference/unityengine/coroutine.md) parameter to stop a coroutine.

```csharp
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;
        }

        // Intended to handling exit of the this coroutine.
        // However coroutineA() shuts coroutineB() down. This
        // means the following lines are not called.
        float t = Time.time - start;
        Debug.Log("coroutineB() finished " + t);
        yield return null;
    }
}
```

## StopCoroutine(Coroutine)

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

```csharp
public void StopCoroutine(Coroutine routine)
```

### Parameters

**** (\[Coroutine]\(/engine/6000.5/script-reference/unityengine/coroutine)): Name of the function in code, including coroutines.

### Remarks

`StopCoroutine` takes one of three arguments that specify which coroutine to stop:

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

You must use the same parameter type to stop a coroutine with `StopCoroutine` as was used to start it with [MonoBehaviour.StartCoroutine](/engine/6000.5/script-reference/unityengine/monobehaviour/startcoroutine.md).

Coroutines also stop if:

* The value of [GameObject.activeSelf](/engine/6000.5/script-reference/unityengine/gameobject/activeself.md) becomes `false` for the GameObject the script is attached to.
* The MonoBehaviour script is destroyed with a call to [Object.Destroy](/engine/6000.5/script-reference/unityengine/object/destroy.md).

**Note**: Disabling the MonoBehaviour script by setting [Behaviour.enabled](/engine/6000.5/script-reference/unityengine/behaviour/enabled.md) to `false` doesn't stop coroutines.

`StopCoroutine(null)` throws a NullReferenceException. Add checks to your code to ensure the argument is not null before calling `StopCoroutine`.

The following example uses the [IEnumerator](https://msdn.microsoft.com/en-us/library/system.collections.ienumerator\(v=vs.110\).aspx) to stop a coroutine.

```csharp
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 example uses the [Coroutine](/engine/6000.5/script-reference/unityengine/coroutine.md) parameter to stop a coroutine.

```csharp
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;
        }

        // Intended to handling exit of the this coroutine.
        // However coroutineA() shuts coroutineB() down. This
        // means the following lines are not called.
        float t = Time.time - start;
        Debug.Log("coroutineB() finished " + t);
        yield return null;
    }
}
```

## StopCoroutine(string)

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

```csharp
public void StopCoroutine(string methodName)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): Name of coroutine.

### Remarks

`StopCoroutine` takes one of three arguments that specify which coroutine to stop:

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

You must use the same parameter type to stop a coroutine with `StopCoroutine` as was used to start it with [MonoBehaviour.StartCoroutine](/engine/6000.5/script-reference/unityengine/monobehaviour/startcoroutine.md).

Coroutines also stop if:

* The value of [GameObject.activeSelf](/engine/6000.5/script-reference/unityengine/gameobject/activeself.md) becomes `false` for the GameObject the script is attached to.
* The MonoBehaviour script is destroyed with a call to [Object.Destroy](/engine/6000.5/script-reference/unityengine/object/destroy.md).

**Note**: Disabling the MonoBehaviour script by setting [Behaviour.enabled](/engine/6000.5/script-reference/unityengine/behaviour/enabled.md) to `false` doesn't stop coroutines.

`StopCoroutine(null)` throws a NullReferenceException. Add checks to your code to ensure the argument is not null before calling `StopCoroutine`.

The following example uses the [IEnumerator](https://msdn.microsoft.com/en-us/library/system.collections.ienumerator\(v=vs.110\).aspx) to stop a coroutine.

```csharp
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 example uses the [Coroutine](/engine/6000.5/script-reference/unityengine/coroutine.md) parameter to stop a coroutine.

```csharp
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;
        }

        // Intended to handling exit of the this coroutine.
        // However coroutineA() shuts coroutineB() down. This
        // means the following lines are not called.
        float t = Time.time - start;
        Debug.Log("coroutineB() finished " + t);
        yield return null;
    }
}
```
