Running coroutines in a recursive function. How to wait until all coroutines done before going to next level

I’ve got a coroutine which moves the position of several gameobjects in a hierarchy. The general structure is like this:

void someFunction()
{
    ...
    if (value)
        someFunction()
    ...
    Func1()
}

void Func1()
{
    StartCoroutine(Func2(root))
}

IEnumerator Func2(Node node)
{
     StartCoroutine(Func2(node.child))
}

It’s a little more nested than that, and there’s a couple of coroutines in there. One is recursive, and one runs on the parent of the current root (if it has one).

I know it’s fairly specific, but in essence, I’d like to wait after the someFunction call inside itself before running any further. I do this to move up an down a tree and traverse it one by one instead of straight to it. Any ideas?

Your someFunction should be an IEnummerator and you should wait for the inner coroutines to be finished using something like this:

    yield return StartCoroutine(Func2(root));

That should only continue to run code when the coroutine it’s calling has finished.