I use coroutines for a lot of my initialization code, yielding frequently to reduce stutter on loading screens, etc.
However, sometimes I want all of my initialization to happen “all at once” (i.e in a blocking fashion, like a normal method, all in one frame). Previously, I have been duplicating my initialization code into coroutine and non-coroutine methods - I’d like to avoid this so I can reduce the amount of code I need to test and debug.
I’ve discovered that I can “run” the coroutine by iterating through it like a normal Enumerator - see code below.
Is what I’m doing supported? I can’t see any disadvantages, or anything that will break, so far in my testing.
Thanks!
void Start()
{
IEnumerator enumerator = MyCoroutine();
while(enumerator.MoveNext())
{
}
Debug.LogError("Complete");
}
IEnumerator MyCoroutine()
{
Debug.LogError("Coroutine Start");
yield return new WaitForSeconds(3.0f);
Debug.LogError("Coroutine End");
}