How To Check if Unity Coroutine Is Running?

Alexey Karimov

Are you experiencing issues with your coroutine triggering multiple times, breaking sequences, or creating unpredictable behavior? These problems often result from not knowing whether a coroutine is still active when your code tries to start it again. This is when you might want to check if a Unity coroutine is running.

Being able to detect an active coroutine gives you better control over your game logic. It helps prevent duplicate executions, manage complex event flows, and make debugging easier. This guide explains different methods to check if a coroutine is running, along with tips for managing them.

free trial banner

What is Unity Coroutine?

Coroutines in Unity are lightweight routines that run on the main thread, not separate threads. They let you pause a block of code and resume it later without freezing the game loop. This makes them ideal for tasks like animations, timed events, and gameplay sequences that need to stretch over multiple frames.

When you start a coroutine, Unity executes the code immediately up to the first yield. After that, the coroutine’s state is stored, and Unity’s scheduler decides when to resume it. The exact timing depends on what you yield:

  • yield return null resumes on the next frame.
  • yield return new WaitForSeconds(x) resumes after the specified delay.
  • yield return new WaitForEndOfFrame() resumes at the end of the current frame.

Checking if a coroutine is still running is useful in many scenarios. It helps prevent duplicate calls when only one instance is running, keeps complex sequences organized, and simplifies debugging by giving clear visibility into coroutine states.

Method 1: Use a Boolean Flag

The simplest way to detect if a coroutine is running is to use a bool variable. You set it to true when the coroutine starts and back to false when it finishes. This approach is easy to implement and works well for smaller scripts or cases where you only need a basic state check. 

Here’s an example:

using UnityEngine;
using System.Collections;

public class CoroutineChecker : MonoBehaviour
{
    private bool isRunning = false;

    void Start()
    {
        StartCoroutine(MyCoroutine());
    }

    IEnumerator MyCoroutine()
    {
        isRunning = true;
        Debug.Log("Coroutine started");

        yield return new WaitForSeconds(2f);
        Debug.Log("Coroutine is halfway done");

        yield return new WaitForSeconds(2f);
        Debug.Log("Coroutine finished");

        isRunning = false;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log(isRunning ? "Coroutine is running" : "Coroutine is not running");
        }
    }
}

In this example, the isRunning flag is toggled automatically by the coroutine. You can check its value at any time in your script to know whether the coroutine is still active. This keeps the logic straightforward without adding extra complexity.

Note: If you stop a coroutine manually, be sure to reset your flag or reference so your checks stay accurate.

Method 2: Track the Coroutine Reference

Another reliable way to check if a coroutine is running is to store the Coroutine object returned by StartCoroutine(). When a coroutine is active, the stored reference isn’t null. Once the coroutine finishes or is stopped, that reference becomes null, making it easy to detect its state.

Here’s an example:

using UnityEngine;
using System.Collections;

public class CoroutineTracker : MonoBehaviour
{
    private Coroutine runningCoroutine;

    void Start()
    {
        runningCoroutine = StartCoroutine(MyCoroutine());
    }

    IEnumerator MyCoroutine()
    {
        Debug.Log("Coroutine started");

        yield return new WaitForSeconds(2f);
        Debug.Log("Coroutine is halfway done");

        yield return new WaitForSeconds(2f);
        Debug.Log("Coroutine finished");

        runningCoroutine = null;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log(runningCoroutine != null ? "Coroutine is running" : "Coroutine is not running");
        }
    }
}

In this example, the runningCoroutine variable holds a reference to the coroutine. You can check whether it’s null to know its state, or even stop it at any time using StopCoroutine(runningCoroutine). This method is more flexible than a boolean flag, especially when you need to manage multiple coroutines or stop them dynamically.

Pro Tip: Storing multiple Coroutine references in a Dictionary<string, Coroutine> can help manage multiple running routines in complex systems.

Wrapping Up

Both methods work well for checking if a coroutine is active, and the right choice depends on the complexity of your project. Boolean flags are perfect for simpler scripts where you only need a quick state check, while storing a coroutine reference gives you more flexibility to stop, restart, or manage multiple coroutines in larger systems.

As a best practice, keep your coroutine management consistent. Use clear names for variables, explicitly stop coroutines when objects are destroyed to avoid leaks, and keep the logic inside your coroutines simple so they’re easy to debug. Sticking to one approach across your project makes your codebase cleaner and easier for you and your team to maintain.