How do I stop coroutine?

I have a coroutine that creates spheres. I want the coroutine to stop after creating 200 spheres. I have a counter in another class that counts the number of spheres created. I have tried using StopCoroutine(), but no luck. Any ideas??

Just for anyone searching in the future, "yield break;" is a real tip, that catches out new coroutiners! it's how you "leave" a coroutine. @ratmstein you should TICK an answer in questions, thanks

@Fattie - thank you for accepting!

2 Answers

2

Please note, that StopCoroutine method will only stop coroutines called with StartCoroutine overload, that accepts coroutine name as string. If you have coroutine:

IEnumerator MyCoroutine()
{
    // content
}

and you call it using

StartCoroutine(MyCoroutine());

then you won’t be able to stop it using

StopCoroutine("MyCoroutine");

For the StopCoroutine method to work, you have to start it using

StartCoroutine("MyCoroutine")

Apart from above, you can stop a coroutine in a different way. For example you can use

IEnumerator MyCoroutine()
{
    for(var i = 0; i < 200; i++)
    {
        // instantiate your sphere
        yield return null;
    }
}

After exiting for loop, coroutine will stop. You can also use yield break to stop execution of coroutin, if specific condition is met:

IEnumerator MyCoroutine()
{
    while(true)
    {
        // instantiate your sphere

        if(/*some condition check*/)
        {
            yield break;
        }
        else
        {
            yield return null;
        }
    }
}

You forgot [StopAllCoroutines][1](). It ends all currently active coroutines, regardless of how they were started. I would also like to add that Coroutines are run on the GameObject. So disabling the gameobject will cause any coroutine running on it to stop. You can also turn on/off coroutines on other GameObjects by gaining a reference to the MonoBehaviour that started it on the GameObject and use Start/StopCoroutine or StopAllCoroutines. [1]: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StopAllCoroutines.html

@Jamora - I tried to describe methods of stopping one particular coroutine only. And you're right that disabling game object is one of them.

I'm getting error messages when I try to name the coroutine with a string. Is it possible to have the method name as well as a string so that I can use StopCoroutine?

When you get any error message, please always include it when you post here. It helps us to identify the problem. As to your problem, please show your coroutine method signature and the line where you try to start it.

I'm not getting an error at the moment, as I have altered my code since my last post. This is my coroutine: public IEnumerator changeDirectionAfterDelay(float seconds) { yield return new WaitForSeconds(seconds); direction = UnityEngine.Random.onUnitSphere; StartCoroutine (changeDirectionAfterDelay(seconds)); }

At some point, StopCoroutine(Coroutine c); was added to the MonoBehavior interface. So it appears you can also do:

 Coroutine theCoroutine = StartCoroutine(MyCoroutine());

and later:

StopCoroutine(theCoroutine);

This worked really well and is exactly what I was looking for.

This is the most appropriate answer.

this is not work