Storing Coroutines inside a list or variable in general?

I’m trying to store some IEnumarators inside non-Monobehaviour and non-static classes, which are taken from a static class, to be later accessed by a Monobehaviour class with the StartCoroutine function. If an instance of the non class gets its stored IEnumerator accessed, it will only successfully run one time and then completely skip the coroutine every other time. If two different instances are using the same coroutine, each will run once and then completely ignore all other tries. If the coroutine is accessed directly from the static class, it will run successfully.

Well, this is pretty much the problem I’ve had, but I’d like to make the question more general. How does one use an IEnumerator as a variable (by making it equal to other existing IEnumerators)? Also, is there a better way to read an IEnumerator from another class?

(I know I’ve written it in a confusing way, I’m sorry for that, but my head hurts from trying to figure this thing out and I can’t exactly focus right now)

The IEnumerator, or the function that generates the IEnumerator?

Because once you start an IEnumerator generated by an iterator function, you can’t reuse it.

I’m willing to be you want to store a reference to the function that creates the IEnumerator used for the Coroutine. In which case you use delegates. In this case… you could just use the System.Func delegate.

void Foo()
{

    System.Func<IEnumerator> delegateRef = SomeRoutine;

}

IEnumerator SomeRoutine()
{
    //do your routine
}

Then with that ref you could call StartCoroutine with it:

StartCoroutine(delegateRef());
1 Like

It works perfectly! Thanks! It did take some tweaking around, but in the end it works! :smile: