DEV Community

Cover image for Unity Coroutine Not Working? 10 Common Problems and Their Solutions
Alok Krishali
Alok Krishali

Posted on Edited on

Unity Coroutine Not Working? 10 Common Problems and Their Solutions

Unity Coroutine not working as expected? You are not alone! Sometimes a coroutine doesn't start, stops unexpectedly, never reaches the next line, or simply doesn't execute after WaitForSeconds().

I have faced these kinds of situations while working on Unity gameplay systems. In many cases, the problem is not with the coroutine itself but with how it is started, stopped, or managed.

For example, you might call StartCoroutine() but never see the expected output. Or your coroutine might wait forever because Time.timeScale is set to zero.

In this article, we will explore 10 common Unity Coroutine problems and their solutions with practical C# examples. We will also understand why these problems happen and how to avoid them in your projects.

Let's get started!

1. Coroutine Is Not Starting

This is one of the most common problems when working with Unity Coroutines.

You create an IEnumerator method, write your logic inside it, and expect Unity to execute it. But nothing happens.

The reason might be that you forgot to call StartCoroutine().

Incorrect approach:

using UnityEngine;
using System.Collections;

public class CoroutineExample : MonoBehaviour
{
    void Start()
    {
        MyCoroutine();
    }

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

        yield return new WaitForSeconds(2f);

        Debug.Log("Coroutine Completed");
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, MyCoroutine() creates an IEnumerator but does not start it through Unity's coroutine scheduler.

Solution:

Use StartCoroutine() to execute it.

void Start()
{
    StartCoroutine(MyCoroutine());
}
Enter fullscreen mode Exit fullscreen mode

Now Unity will execute the coroutine and continue it after the specified delay.

Remember: Calling an IEnumerator method and starting a coroutine are two different things.

2. Coroutine Stops When GameObject Is Disabled or Destroyed

Another common issue occurs when the GameObject running the coroutine gets disabled or destroyed.

For example, imagine you start a coroutine on an enemy GameObject. After 2 seconds, you disable that enemy. You might expect the coroutine to continue, but it will stop.

Example:

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

    yield return new WaitForSeconds(5f);

    Debug.Log("Coroutine Completed");
}
Enter fullscreen mode Exit fullscreen mode

If the GameObject containing this script is disabled or destroyed during the wait, the coroutine will stop.

Solution:

If you need an operation to continue after disabling an enemy or another GameObject, consider running the coroutine from a separate manager GameObject that remains active.

public class CoroutineManager : MonoBehaviour
{
    public static CoroutineManager Instance;

    void Awake()
    {
        Instance = this;
    }

    public void StartMyCoroutine()
    {
        StartCoroutine(MyCoroutine());
    }

    IEnumerator MyCoroutine()
    {
        yield return new WaitForSeconds(5f);

        Debug.Log("Coroutine Completed");
    }
}
Enter fullscreen mode Exit fullscreen mode

Now another script can start the coroutine using:

CoroutineManager.Instance.StartMyCoroutine();
Enter fullscreen mode Exit fullscreen mode

Important: Disabling a GameObject stops its coroutines. However, disabling only the MonoBehaviour component does not automatically stop its coroutines. Destroying the GameObject or unloading its scene also stops them.

3. WaitForSeconds Is Not Working

Sometimes a coroutine starts successfully, but the code after WaitForSeconds() never executes.

One possible reason is Time.timeScale.

For example:

Time.timeScale = 0f;
Enter fullscreen mode Exit fullscreen mode

This pauses scaled game time.

Since WaitForSeconds() depends on scaled time, its countdown will not progress while Time.timeScale is zero.

Example:

IEnumerator MyCoroutine()
{
    Debug.Log("Started");

    yield return new WaitForSeconds(3f);

    Debug.Log("Completed");
}
Enter fullscreen mode Exit fullscreen mode

If the game is paused using Time.timeScale = 0f, the second message will not appear until scaled time starts progressing again.

Solution:

Use WaitForSecondsRealtime() if you want to wait using real time.

IEnumerator MyCoroutine()
{
    Debug.Log("Started");

    yield return new WaitForSecondsRealtime(3f);

    Debug.Log("Completed");
}
Enter fullscreen mode Exit fullscreen mode

When should you use it?

  • Use WaitForSeconds() for gameplay delays.
  • Use WaitForSecondsRealtime() for pause menus, UI messages, and real-time operations that should continue while the game is paused.

4. Coroutine Runs Multiple Times Unexpectedly

Have you ever noticed that the same coroutine starts multiple times?

This usually happens when you call StartCoroutine() repeatedly without checking whether an existing coroutine is already running.

For example:

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        StartCoroutine(MyCoroutine());
    }
}
Enter fullscreen mode Exit fullscreen mode

If the player repeatedly presses Space, multiple instances of the coroutine can run simultaneously.

This may cause unexpected behaviour, especially when handling animations, attacks, or enemy spawning.

Solution:

Store a Coroutine reference and check whether it is already running.

Coroutine myRoutine;

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        if (myRoutine == null)
        {
            myRoutine = StartCoroutine(MyCoroutine());
        }
    }
}

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

    yield return new WaitForSeconds(3f);

    Debug.Log("Coroutine Completed");

    myRoutine = null;
}
Enter fullscreen mode Exit fullscreen mode

Now the coroutine will not start again while the previous instance is running.

This is particularly useful for weapon reloads, attack animations, cooldowns, and timed gameplay events.

5. StopCoroutine Is Not Stopping the Coroutine

Sometimes you call StopCoroutine(), but the coroutine continues running.

This can happen when you use a different reference or overload to stop it than the one used to start it.

For example, using a newly created IEnumerator instance to stop a coroutine that was started using another instance may not stop the intended routine.

Problematic approach:

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

void StopMyCoroutine()
{
    StopCoroutine(MyCoroutine());
}
Enter fullscreen mode Exit fullscreen mode

The second call creates another IEnumerator instance. It is not the same running coroutine.

Solution:

Store the Coroutine reference when starting it.

Coroutine myRoutine;

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

void StopMyCoroutine()
{
    if (myRoutine != null)
    {
        StopCoroutine(myRoutine);
        myRoutine = null;
    }
}

IEnumerator MyCoroutine()
{
    while (true)
    {
        Debug.Log("Coroutine Running");

        yield return new WaitForSeconds(1f);
    }
}
Enter fullscreen mode Exit fullscreen mode

Now you can stop the exact coroutine instance using its reference.

You can also use StopAllCoroutines() to stop all coroutines running on the same MonoBehaviour, but use it carefully.

6. Coroutine Gets Stuck in an Infinite Loop

Another issue is creating an infinite loop inside a coroutine without yielding control back to Unity.

Consider this example:

IEnumerator MyCoroutine()
{
    while (true)
    {
        Debug.Log("Running");
    }
}
Enter fullscreen mode Exit fullscreen mode

This is dangerous!

The loop never gives control back to Unity. It can freeze the main thread and make the game unresponsive.

Solution:

Add a yield statement inside the loop.

IEnumerator MyCoroutine()
{
    while (true)
    {
        Debug.Log("Running");

        yield return null;
    }
}
Enter fullscreen mode Exit fullscreen mode

yield return null pauses the coroutine until the next frame.

You can also use a time delay:

yield return new WaitForSeconds(1f);
Enter fullscreen mode Exit fullscreen mode

Remember: Always make sure that long-running coroutine loops yield control regularly.

7. Coroutine Is Not Reaching the Next Statement

Sometimes your coroutine starts, prints the first message, and never reaches the next statement.

This often happens when you use WaitUntil() or WaitWhile() with a condition that never changes.

Example:

bool isPlayerReady = false;

IEnumerator MyCoroutine()
{
    Debug.Log("Waiting for player...");

    yield return new WaitUntil(() => isPlayerReady);

    Debug.Log("Player Ready!");
}
Enter fullscreen mode Exit fullscreen mode

The coroutine waits until isPlayerReady becomes true.

But if you never update this variable, the coroutine will keep waiting.

Solution:

Make sure the condition is updated correctly.

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        isPlayerReady = true;
    }
}
Enter fullscreen mode Exit fullscreen mode

Now pressing Space changes the condition, allowing the coroutine to continue.

Debugging tip: Add Debug.Log statements before and after WaitUntil() or WaitWhile() to identify where execution gets stuck.

8. Nested Coroutine Is Not Waiting for the Other Coroutine

Sometimes you want to execute one coroutine after another.

For example, you want to complete an animation, wait for 2 seconds, and then spawn an object.

A common mistake is starting another coroutine without yielding it.

Incorrect approach:

IEnumerator MainCoroutine()
{
    StartCoroutine(FirstCoroutine());

    Debug.Log("Main Coroutine Completed");
}

IEnumerator FirstCoroutine()
{
    yield return new WaitForSeconds(3f);

    Debug.Log("First Coroutine Completed");
}
Enter fullscreen mode Exit fullscreen mode

Here, FirstCoroutine() starts separately. The main coroutine does not wait for it to finish.

Solution:

Use yield return StartCoroutine() when you want sequential execution.

IEnumerator MainCoroutine()
{
    yield return StartCoroutine(FirstCoroutine());

    Debug.Log("Main Coroutine Completed");
}

IEnumerator FirstCoroutine()
{
    yield return new WaitForSeconds(3f);

    Debug.Log("First Coroutine Completed");
}
Enter fullscreen mode Exit fullscreen mode

Now the main coroutine waits until the first coroutine completes.

Remember the difference:

  • StartCoroutine() starts another coroutine.
  • yield return StartCoroutine() waits for the nested coroutine to finish before continuing.

9. Coroutine Is Not Running After Scene Change

Have you ever started a coroutine and then changed the scene, only to discover that it stopped?

This is expected behaviour when the GameObject running the coroutine gets destroyed during scene unloading.

For example:

IEnumerator LoadNextLevel()
{
    yield return new WaitForSeconds(3f);

    UnityEngine.SceneManagement.SceneManager.LoadScene("Level2");
}
Enter fullscreen mode Exit fullscreen mode

If the coroutine's GameObject gets destroyed before the delay finishes, the scene loading operation will never happen.

Solution:

Use a persistent manager GameObject with DontDestroyOnLoad().

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour
{
    void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }

    public void StartLevelLoading()
    {
        StartCoroutine(LoadNextLevel());
    }

    IEnumerator LoadNextLevel()
    {
        yield return new WaitForSeconds(3f);

        UnityEngine.SceneManagement.SceneManager.LoadScene("Level2");
    }
}
Enter fullscreen mode Exit fullscreen mode

This allows the manager to survive scene changes, so its coroutine can continue.

Important: Make sure you don't accidentally create multiple persistent GameManagers when loading new scenes.

10. Coroutine Throws an Exception and Stops Unexpectedly

Sometimes a coroutine starts correctly but stops before reaching the expected result.

One possible reason is an exception inside the coroutine.

For example:

IEnumerator MyCoroutine()
{
    Debug.Log("Started");

    GameObject enemy = null;

    enemy.SetActive(false);

    yield return new WaitForSeconds(2f);

    Debug.Log("Completed");
}
Enter fullscreen mode Exit fullscreen mode

Here, enemy is null, so accessing SetActive() throws a NullReferenceException.

The remaining coroutine statements will not execute normally.

Solution:

Check references before using them.

IEnumerator MyCoroutine()
{
    Debug.Log("Started");

    if (enemy != null)
    {
        enemy.SetActive(false);
    }
    else
    {
        Debug.LogWarning("Enemy reference is missing!");
        yield break;
    }

    yield return new WaitForSeconds(2f);

    Debug.Log("Completed");
}
Enter fullscreen mode Exit fullscreen mode

Also, check the Unity Console for errors and stack traces whenever a coroutine behaves unexpectedly.

My recommendation: Don't ignore Console errors. A small null reference or missing component can be the reason your entire gameplay sequence fails.

Bonus: Quick Unity Coroutine Debugging Checklist

Before spending too much time debugging, check these common points:

  • Is the coroutine started using StartCoroutine()?
  • Is the GameObject active?
  • Has the coroutine been stopped somewhere else?
  • Is Time.timeScale set to zero?
  • Is a WaitUntil() condition actually becoming true?
  • Are you accidentally starting multiple coroutine instances?
  • Does every infinite loop contain a yield statement?
  • Are you using the correct Coroutine reference?
  • Is the coroutine's GameObject destroyed during a scene change?
  • Are there any exceptions in the Unity Console?

These simple checks can help you identify many coroutine problems quickly.

Conclusion

Unity Coroutines are extremely useful for handling delays, animations, enemy spawning, countdowns, and other time-based gameplay operations.

However, understanding how they work is just as important as knowing how to write them.

Most Unity Coroutine problems are caused by incorrect starting or stopping, GameObject lifecycle issues, time scale settings, missing yield statements, or conditions that never become true.

Let's quickly revise everything we learned.

Quick Summary: 10 Common Coroutine Problems

  1. Coroutine not starting: Always use StartCoroutine() to start an IEnumerator routine.
  2. GameObject disabled: Coroutines stop when their GameObject is disabled or destroyed.
  3. WaitForSeconds not working: Check Time.timeScale; use WaitForSecondsRealtime() when needed.
  4. Multiple executions: Store Coroutine references and prevent unnecessary duplicate starts.
  5. StopCoroutine not working: Stop the correct running coroutine instance.
  6. Infinite loop: Always yield control inside long-running loops.
  7. Waiting forever: Check your WaitUntil() and WaitWhile() conditions.
  8. Nested coroutine issues: Use yield return StartCoroutine() for sequential execution.
  9. Scene change issues: Use a persistent manager when an operation must survive scene unloading.
  10. Unexpected exceptions: Check references and inspect Unity Console errors.

Do's and Don'ts When Using Unity Coroutines

Do:

  • Use StartCoroutine() to execute coroutine methods.
  • Store Coroutine references when you need to manage them.
  • Use WaitForSecondsRealtime() for real-time delays.
  • Add yield statements inside long-running loops.
  • Check conditions before using WaitUntil() and WaitWhile().
  • Use Unity Console logs to debug unexpected behaviour.
  • Keep coroutine management organized.

Don't:

  • Don't call an IEnumerator method and expect it to run automatically.
  • Don't start the same coroutine repeatedly without checking its state.
  • Don't use infinite loops without yielding.
  • Don't forget that Time.timeScale affects WaitForSeconds().
  • Don't assume disabling a MonoBehaviour stops its coroutines.
  • Don't ignore NullReferenceException errors.
  • Don't use StopAllCoroutines() when you only need to stop one specific routine.

Frequently Asked Questions (FAQs)

1. Why is my Unity Coroutine not starting?

The most common reason is forgetting to call StartCoroutine(). Also check whether the GameObject is active, the script is enabled, and there are any errors in the Unity Console.

2. Does disabling a GameObject stop a Coroutine in Unity?

Yes. Disabling the GameObject stops its running coroutines. Destroying the GameObject also stops them. However, disabling only the MonoBehaviour component does not automatically stop its coroutines.

3. Why is WaitForSeconds not working in Unity?

Check whether Time.timeScale is set to zero. WaitForSeconds() uses scaled game time, so it will not progress while the game is paused. Use WaitForSecondsRealtime() if you need an unscaled delay.

4. How can I prevent a Unity Coroutine from running multiple times?

Store its reference in a Coroutine variable and check whether it is already running before calling StartCoroutine() again. Set the reference to null when the coroutine finishes.

5. Can I run multiple Coroutines at the same time in Unity?

Yes! Unity allows you to run multiple coroutines simultaneously. Each coroutine can manage its own execution and wait instructions. However, make sure you manage their references and stop unnecessary routines.


Thanks for reading!

I hope these 10 common Unity Coroutine problems and their solutions help you debug your projects more easily.

If you found this article useful:

  • 👍 Give it a like to show your support.
  • 🔄 Share it with other Unity developers who might find it helpful.
  • 💬 Leave a comment if you have faced a different coroutine problem or have another solution to share.
  • 🚀 Follow us for more practical Unity tutorials, C# tips, and game development guides.

Have a great time building and debugging your games!

Happy Game Development! 🎮

Top comments (0)