IEnumerator in Update

I am just wondering if you can run an IEnumerator (or any iterator) within Update (or an Update called from a mono Update). I am not inheriting from Monodevelop, so I cannot use the function StartCoroutine().

Or should I just reference a mono script and use it’s StartCoroutine()?

Edit 1: I am specifically wondering if I can yield a time value (as I can iterate through it with MoveNext(), but it doesn’t yield to time yields).

Yes you can iterate over a collection of IEnumerable objects, since without Unity’s MonoBehaviour class or yield instruction you can always go back to basics:

  1. Define an IEnumerator derived class that contains your list of IEnumerable objects
  2. Call it with a basic loop, eg

foreach (EnumerablePerson p in peopleList){…}

The official docs provide a more complete sample.

Yes, you can start coroutines from any method as long as the call is issues on the main thread. If a function is called from Update / Start / Awake / … it’s fine to start a coroutine there.

StartCoroutine however is a method which belongs to the MonoBehaviour class. Coroutines (which are objects, not really methods) are always run on a gameobject instance. So you have to use a reference to any MonoBehaviour instance to use StartCoroutine. Keep in mind that the coroutine will be bound to that MonoBehaviour instance. It it is destroyed the coroutine will be stopped.