- Home /
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 TIC$$anonymous$$ an answer in questions, thanks
Answer by ArkaneX · Sep 15, 2013 at 09:38 PM
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 $$anonymous$$onoBehaviour that started it on the GameObject and use Start/StopCoroutine or StopAllCoroutines.
[1]: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.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.
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.
You can then call it using
StartCoroutine("changeDirectionAfterDelay", seconds);
Please note that you have to change last line of your coroutine to above line as well.
And you can stop it using
StopCoroutine("changeDirectionAfterDelay");
Ins$$anonymous$$d of calling the coroutine again at the end of execution, you can also change it to:
public IEnumerator changeDirectionAfterDelay(float seconds)
{
while(true)
{
yield return new WaitForSeconds(seconds);
direction = UnityEngine.Random.onUnitSphere;
}
}
Answer by ObviousDWest · Apr 07, 2017 at 10:44 AM
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.
Your answer

Follow this Question
Related Questions
For Loop Won't Stop Running... 1 Answer
I can't create a world border? 1 Answer
Particle system stop? 3 Answers
How to make Timer stop upon Death? 1 Answer
Stop player movement if it's Raycast hits an object 0 Answers