If I call Run() multiple times, I expect to see the messages "One", "Two" and "Three", but it's not what is happening, I can see the first one and then the MoveNext() returns false. What am I missing here?
public class Test
{
private IEnumerator<Object> routine;
public void Setup()
{
routine = CountOne();
}
public void Run()
{
routine.MoveNext();
}
private IEnumerator<Object> CountOne()
{
Console.WriteLine("One");
yield return CountTwo();
}
private IEnumerator<Object> CountTwo()
{
Console.WriteLine("Two");
yield return CountThree();
}
private IEnumerator<Object> CountThree()
{
Console.WriteLine("Three");
yield return null;
}
}
It seems that this comes from Unity and I have not worked with it for a while, but try enumerating the enumerators:
Otherwise in
CountOneyou just return a single item which is the next enumerator without actually processing it (i.e. return type ofCountOneis effectivelyIEnumerator<IEnumerator>).yield returnsupports returningIEnumeratorandIEnumerableinterchangeably. And from the docs: