StopCoroutine doesn’t seem to stop nested coroutines. For example:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
private void Start()
{
StartCoroutine("Parent");
}
private void Update()
{
if (Input.GetKeyUp(KeyCode.S))
{
Debug.Log(Time.frameCount + " stop");
StopCoroutine("Parent");
}
}
private IEnumerator Parent()
{
while (true)
{
yield return StartCoroutine("Child");
}
}
private IEnumerator Child()
{
yield return new WaitForSeconds(1);
Debug.Log(Time.frameCount + " test");
}
}
Since the Child coroutine was started from Parent I would expect that stopping Parent would also stop Child. However, it seems that Child continues to run until it’s done.
Is there any (nice) way to stop nested coroutines?
StopAllCoroutines would work in this example but there are situations where I’m running multiple coroutines on an object and I don’t want to stop all of them. I want to stop a specific coroutine and any nested coroutines it started.
I’m pretty sure the answer is that it can’t be done (at least not with any built-in APIs). Maybe this is more of a feature request.
It depends on your setup. You should maintain the flow if your script. If you got a coroutine starting, let it create a list to store all the others. Thats what a built in thing would do. But as it is content/script architecture related, it is not