Calling IEnumerators without StartCoroutine?

I remember this one was working pre Unity5:

using UnityEngine;
using System.Collections;

public class testing : MonoBehaviour {
  
    void Start () {
        test();
    }

    private IEnumerator test () {
        Debug.Log("test");
        yield break;
    }
}

Unfortunately no error is thrown here, but I could swear that this worked in the past.

edit: it seems with test().MoveNext(); it’s working

1 Like

MoveNext() will bring it to the first ‘yield’ statement. You’ll have to iterate until MoveNext returns false, if you want the entire method to process.

var e = test();
while(e.MoveNext()) { }

Of course, most coroutine iterators expect the yield statements to wait until the next frame. So if you have one that can result in infinite yielding, or really long processes, this will most certainly resulting in blocking/freezing the program.

I used custom coroutines like this for one of my projects. I basically created a system of “Yield until the user meets certian objectives”. Made for an interesting approach to a quest system.