- Home /
Determining the status of a Coroutine
is there any way to see what the current status of a coroutine is? for example if I am driving my GUI based on the current status of a coroutine display X while coroutine is at PointX, and display Y while coroutine is at PointY, and display Z when coroutine is finished.
I know that you could probably do this with globals, but I am more thinking of actually seeing what the last thing yield returned by the coroutine was.
Answer by vbbartlett · Feb 21, 2013 at 01:37 AM
Only way would be to have the script maintain a state variable and update it in the coroutine. The script has access to its own variables whether in the Update() or in a coroutine.
class Example:MonoBehaviour {
int m_state;
IEnumerator CoroutineFunction ()
{
m_state = 0;
// do something
yield return null;
m_state++;
// do something else
yield return null;
m_state++;
// do something else
yield return null;
m_state++;
// do something else
yield return null;
}
void Update()
{
switch(m_state)
....
// or
if(m_state == *desiredState*)
...
}
that is essentially/exactly what I meant by "global" (a variable that exists outside of a method, or main). I was thinking that because the keyword return
is used that a value would be visible with regard to the IEnumerator
Well, you could probably use reflection on the IEnumerator object you used to start the coroutine, but that's not really a good approach since the class is auto-generated by the compiler and is a private class.
@Bunny83 a little more explanation, and that is probably the answer to the question (whether I like it or not)