Hi,
Consider the following code:
private Coroutine stopMeRoutine;
private void Start()
{
StartCoroutine(Test());
}
IEnumerator Test()
{
while (true)
{
stopMeRoutine = StartCoroutine(StopMe());
yield return stopMeRoutine;
Debug.Log("Next!");
}
}
IEnumerator StopMe()
{
while (true)
{
Debug.Log("Running...");
yield return new WaitForSeconds(1);
}
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Stopping!");
StopCoroutine(stopMeRoutine);
}
}
I would expect “Next!” to be logged after a click, however, Test() hangs indefinitely after a click.
Seems like yield return stopMeRoutine never knows about the coroutine being stopped externally, which is weird because they are the same coroutine instance.
Is this expected behaviour? Any ways around this?