- Home /
http://answers.unity3d.com/questions/1182632/problem-with-stopping-nested-coroutines-control-ne.html
Nested Coroutines > 'Parent Loop' breaks when I stop the nested routine.
I might very well be using the wrong terminology and this might all be a very wrong approach but what is happening is that I do have a coroutine state machine with a main loop using while(true) and yield return StartCoroutine. Everything concerning the looping through the current state works fine but now I have a transition loop outside of the statemachine that watches out for evens that trigger a new state at any point. To make that break happen I first store the coroutine in a variable to stop it specifically inside this transition method. The state gets changed and the state coroutine does get stopped, but the original 'parent loop' also stops. I fixed it by just restarting the loop after stopping the nested state but it seems like a dirty fix - SOOO why does this happen in the first place? ^^
public State currentState;
Coroutine currentCoroutine;
IEnumerator FSM ()
{
while (true)
{
yield return currentCoroutine = StartCoroutine (currentState.ToString ());
}
}
void SwitchState()
{
StopCoroutine (currentCoroutine);
currentState = State.Chase;
StartCoroutine (FSM ());
}