Hi All -
I’m encountering a situation where my coroutine start block is called, but is never entered. While debugging, I discovered that after stepping-in from a breakpoint set at MyCoroutine(), it steps into the { but then exits out to an FixedUpdate() in another class. It never returns to the MyCoroutine() function.
Any thoughts?
yield return StartCoroutine(_state); Pretty sure that's what you'd want to do. You have to return the coroutine somehow, but I can't quite remember if that is the correct way.
I don't know Coroutines particularly well so I won't say this in an answer... You aren't starting MyCoroutine as a Coroutine, you are just calling it like a regular function. Unity won't know to call this again on your behalf unless you use StartCoroutine(MyCoroutine()). You are also starting another Coroutine within MyCoroutine, which doesn't seem like a good idea unless you really know what you are doing.
@tw1st3d - Took your recommended line (Line 20), still acts the same. @Huacanacha - Also took your advice, and started the coroutine in Start() (Line 4). Still acts the same way. :(
What exactly are you trying to do? Any why are you starting a coroutine from inside another coroutine? If you tell us exactly what you want to achieve maybe we can help... the fact that the code steps out of the coroutine is expected, as that's what the 'yield' does. Maybe try 'yield return null' to see what that does. I'm flying a little blind here, so maybe someone with better coroutine knowledge can give their advice!
yield return null; will make your script return to this line every frame and jumps back to the begining of the while loop to check if(true) (which it is true), then it will run the code up to (yield return null)
Of course not the only way to use coroutine, I know that. But his question was about running this code every frame, it's obvious since he was trying to get back to the string, I think he was using it kinda like (Goto) and the broken link was Unity Gems itself...lol http://unitygems.com/coroutines/
No problem, you're just trying to help. I don't think the OP said anywhere they want to run the coroutine every frame, they said they are looking to make a finite state machine.
I was able to trouble shoot the problem… There were 2 fixes I made to get the FSM working correctly.
Syntax Problem - I was using IENumerable instead of `IENumerator’.
Restructured my FSM, granted, it seems like a hack fix making a coroutine manage another coroutine that is executed and terminated based on the state that is called. (See Below)
// 1. Created an enum to hold all states available.
enum State { StateNewGame,
StateBall1,
StateBall2,
StateBall3,
StateRolling,
StateRolledPast,
StateGutterBall,
StateNextBall,
StateSpare,
StateStrike,
StateKnockedSomeDown,
StateRollOver,
StateGameOver };
public State _state;
void Awake()
{
_state = State.StateNewGame; // 2. Assign the first state.
StartCoroutine(MyCoroutine()); // 3. Start the initial Coroutine to start.
CreatePins();
}
void Start()
{
Debug.Log("START CALLED");
}
IEnumerator MyCoroutine()
{
while (true)
{
Debug.Log ("State "+_state.ToString());
yield return StartCoroutine(_state.ToString()); // 4. Initializes a new Coroutine for the state that is called.
}
}
The FSM works now, but I’m in the process of debugging the error I get.
Good news! Sanity check: You do have an IEnumerator method called StateKnockedSomeDown defined? And it's accessible from where you call StartCoroutine to change state?
Haha! Yea, all those check off. Found the bug. Had to do with me attempting to call a state that should not have been called within StateBall3. Hurrah for flow bugs now that everything is working correctly! Thank you again @Huacanacha !
yield return StartCoroutine(_state); Pretty sure that's what you'd want to do. You have to return the coroutine somehow, but I can't quite remember if that is the correct way.
– tw1st3dI don't know Coroutines particularly well so I won't say this in an answer... You aren't starting MyCoroutine as a Coroutine, you are just calling it like a regular function. Unity won't know to call this again on your behalf unless you use StartCoroutine(MyCoroutine()). You are also starting another Coroutine within MyCoroutine, which doesn't seem like a good idea unless you really know what you are doing.
– Huacanacha@tw1st3d - Took your recommended line (Line 20), still acts the same. @Huacanacha - Also took your advice, and started the coroutine in Start() (Line 4). Still acts the same way. :(
– VizionzDoes the debug log fire?
– tw1st3dWhat exactly are you trying to do? Any why are you starting a coroutine from inside another coroutine? If you tell us exactly what you want to achieve maybe we can help... the fact that the code steps out of the coroutine is expected, as that's what the 'yield' does. Maybe try 'yield return null' to see what that does. I'm flying a little blind here, so maybe someone with better coroutine knowledge can give their advice!
– Huacanacha