WaitForSeconds not working in IEnumerator function using MoveNext

I want to start N number of coroutines and wait for them all to resolve. The following seems to work with the exception of WaitForSeconds.

I have the following code with two DoSomething functions A and B I expect both to cause a wait of 5 seconds but only DoSomethingA waits the 5 seconds. DoSomethingB returns right away.

IEnumerator DoSomethingA()
{
  float time = Time.time;
  while (Time.time <= time + 5)
  {
    yield return null;
  }
}

IEnumerator DoSomethingB()
{
  yield return new WaitForSeconds(5);
}

IEnumerator Start () {
  List<IEnumerator> waiting = new List<IEnumerator>();

  waiting.Add(DoSomethingA());
  waiting.Add(DoSomethingA());
  waiting.Add(DoSomethingA());
  waiting.Add(DoSomethingB());
  waiting.Add(DoSomethingB());
  waiting.Add(DoSomethingB());

  while(waiting.Count>0){
  	for (int i = 0; i < waiting.Count; i++)
  	{
  	  IEnumerator e = waiting*;*

if (!e.MoveNext()) {
waiting.Remove(e);
i–;
Debug.LogWarning("Done "+Time.time);
}

  • }*
  • yield return null;*
    }
    }
    Any idea why this is?

All your DoSomething methods are not coroutines since you don’t pass them to Unity’s coroutine scheduler. They are just pure “generator methods”. The point of a generator is usually to produce “things” which you can access through the Current property of the IEnumerator object after you called MoveNext.

Unity just uses generators and basically flip the logic around. Usually the things that you yield are what you’re actually interested in. Between the yields there can be additional code which might be required for the generation of the content that should be returned.

A coroutine basically does the opposite. The main focus of a coroutine is the “code in between” yields. The yields are just cooperative yielding points where the control is yielded back to the caller. The actual “content” that is produced (the things that you yield) are simply used by the scheduler to determine when they have to continue this coroutine.

By iterating the IEnumerators yourself you basically take the spot of the coroutine scheduler. If that’s not what you wanted you may want to actually start the coroutines with StartCoroutine, Since you want to wait for all coroutines this usually works:

List<Coroutine> waiting = new List<Coroutine>();

waiting.Add( StartCoroutine( DoSomethingA() ) );
waiting.Add( StartCoroutine( DoSomethingA() ) );
waiting.Add( StartCoroutine( DoSomethingA() ) );
waiting.Add( StartCoroutine( DoSomethingB() ) );
waiting.Add( StartCoroutine( DoSomethingB() ) );
waiting.Add( StartCoroutine( DoSomethingB() ) );

for (int i = 0; i < waiting.Count; i++)
    yield return waiting*;*

Debug.Log(“all done”);
Of course you don’t get feedback when the individual coroutines finish. If the first one takes the longest time to finish, all others will have finished before the first one. So that means once the longest has finished the for loop will “rush” through the others as they are finished already.
Anyways the debug log at the end is executed once all coroutines have completed.
Though if you want to create / use your own coroutine scheduler you can create one or use [this alternative][1]. Note that the “CoroutineScheduler” on the wiki is not “compatible” with Unity’s coroutines as most of Unity’s special yield values have the important information buried in private / internal or native fields.
The autor of that neat little CoroutineScheduler wrote an excelent blog article where he explains how an IEnumerator and Unity’s coroutines work, Unfortunately the blog doesn’t exist anymore. However we have the “wayback machine”, so [here’s the cached article][2].
If you don’t want to create your own coroutine scheduler / system but you want to get feedback after every coroutine, you might want to try my [CoroutineHelper][3]. It can “wrap” any coroutine in another coroutine and allows to give feedback when it’s instance has finished. It runs all coroutine on a seperate singleton class to ensure they aren’t destroyed too early. You can even “register” a callback that is called when the coroutine is done. Though it might be a bit overkill for what you want. Alternatively you can create your own “wrapper” like this:
public class CoroutineEx
{
private IEnumerator m_Enumerator;
private bool m_IsDone = false;
public bool IsDone { get { return m_IsDone;}}
public CoroutineEx(IEnumerator aCoroutine)
{
m_Enumerator = aCoroutine;
}
public IEnumerator Run()
{
while (m_Enumerator.MoveNext())
yield return m_Enumerator.Current;
m_IsDone = true;
}
}
With that class you can do it like this:
List waiting = new List();

waiting.Add(new CoroutineEx( DoSomethingA() ));
waiting.Add(new CoroutineEx( DoSomethingA() ));
waiting.Add(new CoroutineEx( DoSomethingA() ));
waiting.Add(new CoroutineEx( DoSomethingB() ));
waiting.Add(new CoroutineEx( DoSomethingB() ));
waiting.Add(new CoroutineEx( DoSomethingB() ));

for(int i = 0; i < waiting.Count; i++)
StartCoroutine(waiting*.Run()); // start each coroutine with the wrapper.*

while(waiting.Count > 0)
{
for (int i = waiting.Count-1; i >= 0; i–)
{
if (waiting.)
{
waiting.RemoveAt(i);
Debug.LogWarning("Done "+Time.time);
}
}
yield return null;
}
Debug.Log(“All done”);
_[1]: http://wiki.unity3d.com/index.php?title=CoroutineScheduler*_
_
[2]: https://web.archive.org/web/20130709230443/http://www.cpudreams.com/2010/02/coroutines.html*_
_*[3]: http://wiki.unity3d.com/index.php/CoroutineHelper*_