- Home /
Stop unnamed Coroutine instance
When I start a Coroutine like this
StartCoroutine(MyCoroutine());
Is there any way to stop it again? And if not, what is the point of the Coroutine
-Class?
edit
MyCoroutine
has parameters, so it actually looks like this:
StartCoroutine(MyCoroutine(a, b, c));
And I might have multiple instances of the Coroutine running simultaniously.
The only solution I could come up with is to dynamically add empty MonoBehavior
s to the GameObject
and run each Coroutine in that behavior. But that seems very dirty to me (I did not yet actually try this).
Also I wonder what the point of the Coroutine
-Class is.
Answer by Key_Less · Nov 28, 2013 at 01:19 AM
You can check out the Unity script reference page for StopAllCoroutines() here.
With this approach, just be aware that calling StopAllCoroutines() will stop all coroutines you have running on that behavior, which may lead to undesired results if you have more than one coroutine running. If you want to stop that individual coroutine, then you will need to call and stop it like this:
StartCoroutine("MyCoroutine");
StopCoroutine("MyCoroutine");
You can read more about coroutines here.
Thank you. I already know that, but my coroutines are parameterized and thus I cannot use the string variant of StartCoroutine
and thus I asked if there is a way of stopping Coroutines by their handle, and if not why they have a handle at all, because I cannot find any $$anonymous$$ethod taking it as parameter.
You are incorrect. If you read the link to Unity's Script Reference page regarding coroutines, you'd see that calling a coroutine using the string method allows you to pass in a parameter. Although it only allows for one parameter, it is of type object. $$anonymous$$eaning you can pass in any predefined or user-defined types. You could, for example, pass in a list of game objects or perhaps a struct that contains all the data you want to use in your coroutine.
// ...
var myStruct = new $$anonymous$$yStruct();
// Initialize data in struct.
// ...
// Pass your struct into the coroutine.
StartCoroutine("$$anonymous$$yCoroutine", myStruct);
Now you can access the data from your struct.
public IEnumerator $$anonymous$$yCoroutine($$anonymous$$yStruct data)
{
// Access your struct here
// Ex: data.myStructData;
yield return null;
}
This is just an example of what you could do. I don't know what you are using as parameters.
Sorry, I was unclear. Yes, I can use it to start the coroutines, but if i have started the same coroutine twice, but with different parameters, how can I tell which ones to stop?
Calling stop on a coroutine will effect only the coroutines that are running on that particular behaviour. I need to ask, are you calling the same coroutine once from multiple objects, or are you calling the coroutine multiple times from only one object?
I don't know of any way to keep track of and stop a specific coroutine out of multiple instances. Perhaps we can find another solution to your issue if you shed some light on why you are calling the same coroutine multiple times?
I use them for animations. Yes several run on the same Behavior, see the edit of my initial question.
Your answer
