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?
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;
}
}
checkout huacanacha’s answer at :
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);