- Home /
StopCoroutine with IEnumerator not working
So Unity just added functionality to stop coroutines with an IEnumerator instead of just a string. However it is not working for me. Has anyone else had this issue?
@yeagz7 do you still have this problem? Did you find any way to fix it? I'm currently in the same situation.
Answer by jenci1990 · Dec 03, 2014 at 11:17 PM
It's works, if you start the corutine with string:
public class Test : MonoBehaviour {
void Start() {
StartCoroutine("Printer", "ds"); //Use string for method name
}
void Update() {
if (Input.GetKeyDown(KeyCode.A)) {
StopCoroutine("Printer");
}
}
IEnumerator Printer(string _test) {
float f = 1;
while (f > 0f) {
Debug.Log(_test);
yield return new WaitForEndOfFrame();
}
yield return false;
}
}
I know you can stop the coroutine with a string but in 4.6 they added functionality to stop it with the method signature. So:
public class Test : $$anonymous$$onoBehaviour {
void Start() {
StartCoroutine(Printer(), "ds"); //Use string for method name
}
void Update() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A)) {
StopCoroutine(Printer());
}
}
IEnumerator Printer(string _test) {
float f = 1;
while (f > 0f) {
Debug.Log(_test);
yield return new WaitForEndOfFrame();
}
yield return false;
}
}
I'm not getting any errors. The coroutine is just not stopping.
It's worked, but i don't like it:
public class Test : $$anonymous$$onoBehaviour {
public Coroutine coroutine;
void Start() {
coroutine = StartCoroutine("Printer", "ds");
}
void Update() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A)) {
StopCoroutine(coroutine);
}
}
IEnumerator Printer(string _test) {
float f = 1;
while (f > 0f) {
Debug.Log(_test);
yield return new WaitForEndOfFrame();
}
yield return false;
}
}
It makes sense though, you could think of a coroutine as being an instance when it's launched. The engine would have no idea which coroutine is being referenced as many could exist simultaneously, unless you pass the exact instance you want to stop. It's possible that when started/stopped as a string, only one instance can exist, I'm not sure.
Answer by honor0102 · Apr 26, 2020 at 09:14 PM
checkout huacanacha's answer at :
https://answers.unity.com/questions/934490/stopcoroutine-is-not-stopping-my-coroutines.html
according to that answer:
In more recent versions of Unity (at least 5.3 onwards) you can keep a reference to the IEnumerator or returned Coroutine object and start and stop that directly, rather than use the method name. These are preferred over using the method name as they are type safe and more performant. See the StopCoroutine docs for details.
Coroutine lastRoutine = null;
lastRoutine = StartCoroutine(YourCoroutine());
// [ ... ]
StopCoroutine(lastRoutine);
Your answer
Follow this Question
Related Questions
Coroutines not passing yield 1 Answer
Collection Change During Iteration 0 Answers
Backspace typewriter effect 1 Answer
Make a function that can return string or IEnumerator 2 Answers
Coroutine Not Working 2 Answers