- Home /
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).
If you aren't inheriting from $$anonymous$$ono, there is no Update(). . . ?
So your question is lacking any substance for us to answer on. Can you pose your question in a different way? Such as what you are trying to do and what is not working?
Like the others said, your question doesn't make much sense. First you have to distinguish between a simple IEnumerator or IEnumerable and a coroutine (which also returns an IEnumerator but is a different concept). So which one do you talk about? What "other iterators" do you have in $$anonymous$$d?
I guess you ment your class isn't derived from $$anonymous$$onoBehaviour and like getyour411 said, there is no Update in this case. Even when you implement a method and call it Update it has nothing to do with Unity's Update callback.
So you should sort out your question and be a bit more precise about what you want to do.
I call a non mono "Update" within a mono "Update". And basically, all I want to know is if there is a way to yield or delay an IEnumerator within the non mono class (because I can iterate through it, but it ignores my time yields)...
I haven't checked, but I think StartCoroutine is a UnityEngine call, not a $$anonymous$$onoBehaviour class function. Try using UnityEngine;
Answer by riazrizvi · Apr 01, 2014 at 03:33 PM
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:
Define an IEnumerator derived class that contains your list of IEnumerable objects
Call it with a basic loop, eg
foreach (EnumerablePerson p in peopleList){...}
The official docs provide a more complete sample.
Answer by Bunny83 · Apr 01, 2014 at 06:31 PM
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.
Yeah, I figured as much. Are you saying that yielding (eg. WaitForSeconds) is only available when inheriting from mono?
You can only yield in a function which is declared as IEnumerator - you can have that inside anything but you will have to call StartCoroutine on a $$anonymous$$onoBehaviour somewhere. So you IEnumerator can be a library or utility function but it will be bound to a GameObject so it knows when to run.
Your answer
Follow this Question
Related Questions
How to make a 3d text writeOut method? 1 Answer
Can I use StartCoroutine inside of the same coroutine with a new variable? 0 Answers
Object Not Instantiating After Being Destroyed 0 Answers
why is monodevelop not loading/opening unity 5.1.1f1 0 Answers
StartCoroutine and IEnumerator's - Multiple usage of function 0 Answers