Running a coroutine without StartCoroutine

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.


	void Start()
		IEnumerator enumerator = MyCoroutine();

	IEnumerator MyCoroutine()
		Debug.LogError("Coroutine Start");
		yield return new WaitForSeconds(3.0f);
		Debug.LogError("Coroutine End");

The problem is that it open a door for possible bugs. Each coroutine return type have a meaning, and you are not respecting it. For example, I can insert yield return 0 after create many objects so I only continue after everything get fully initialized.

If is not causing problem and you know what are you doing, go on, no time to waste :stuck_out_tongue:

In the long term a proper abstraction for lazy load should be a better idea, so you can control loads per time, have progress, retry, etc.