- Home /
Stop all instances of same coroutine?
So I call the same coroutine multiple times, which creates multiple instances of the coroutine. This works perfectly, the issue arrises when I want to stop ALL of the instances of that coroutine. I have other coroutines running simultaneously so the StopAllCoroutines wont be of use to me(Unless there's a way to specify it to a specific coroutine "type"). I also don't know how many instances of said coroutine there are as they are generated by the program itself. For Instance:
void Start(){
StartCoroutine("SomethingImportant");
}
void Update(){
//If you press the W key, an instance of "TheCoroutine" will be initiated
if(Input.GetButtonDown(KeyCode.W)){
StartCoroutine("TheCoroutine");
}
//If you press the S key, all the previous instances of "TheCoroutine" will be destroyed
if(Input.GetButtonDown(KeyCode.S)){
//This is the part where I want to end all instances of "TheCoroutine"
//without ending "SomethingImportant"
}
}
IEnumerator TheCoroutine(){
//Do something
//This may be called multiple times which creates multiple instances
//of this coroutine
}
IEnumerator SomethingImportant(){
//Something important that I don't want to stop
}
Answer by Casiell · Oct 18, 2018 at 06:34 AM
StartCoroutine returns an object of type Coroutine. You can keep them in a list and then close all of them with StopCoroutine.
I would have to test what happens when you use string arguments to start and stop a coroutine. Because with IEnumerator and Coroutine argument types you have a reference to a specific coroutine, but with string you do not, so I wonder what happens.
Edit: Ok, if you are using string arguments for starting, you can also use a string for stopping and it will stop all coroutines with this name. Here is a simple code that I created to test that:
public class Coroutines : MonoBehaviour
{
private readonly WaitForSeconds _wait = new WaitForSeconds(0.1f);
private int _i;
private void Start()
{
for (int i = 0; i < 10; i++)
{
_i = i;
StartCoroutine("Coroutine");
}
}
public void StopCoroutines()
{
StopCoroutine("Coroutine");
}
private IEnumerator Coroutine()
{
int i = _i;
while (true)
{
Debug.Log(i.ToString());
yield return _wait;
}
}
}
[CustomEditor(typeof(Coroutines))]
public class CoroutinesInspector : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Stop coroutines"))
{
((Coroutines)target).StopCoroutines();
}
}
}
Thanks for your reply! I ended up just making a list of the IEnumerators then just using StopCoroutine in a loop to stop them. Though it is good to know that the string version of StopCoroutine stops all of the coroutines with that name.
Your answer

Follow this Question
Related Questions
Do debug.log messages show up in unity console in exactly the sequence those statements are called? 0 Answers
How can I call functions in multiple scripts, if they have the same name? 1 Answer
Unable to change a variable from another script. 2 Answers
How can I slowly rotate a vector2 to track a target and give velocity based on the angle. 1 Answer