Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / game

How to Create a Recursive Call with Unity’s Coroutines

4.43/5 (4 votes)
19 Feb 2015CPOL 15K  
How to create a recursive call with Unity's coroutines

During the development of Super Stems, I’ve had to deal with chain reaction. I started a battle with one tile, and if they win, the captured tile would start a battle, and this would happen recursively. For some reason, I have this battle method call on a different thread, in Unity’s terms, a Coroutine.

Now comes the question. How do I handle recursion with Coroutines? After looking around the doc and experimenting, I found a solution.

Here’s a sample code on how to make it work.

C#
public void StartBattle( )
{
    StartCoroutine(BattleRecursive(0));
}

public IEnumerator BattleRecursive( int depth )
{
    // Start Coroutine"
    
    yield return new WaitForSeconds(2f);

    if( depth == 0 )
        yield return StartCoroutine( BattleRecursive(depth+1) );

    if( depth == 1 )
        yield return StartCoroutine( BattleRecursive(depth+1) );
    
    Debug.Log( "MyCoroutine is now finished at depth " + depth );
}

After calling the entry point to your recursive method StartBattle, inside, you have yield return a new Coroutine call to the recursive method. This way, the execution order will be correct. Try it out and see for yourself.

The output should be:

// MyCoroutine is now finished at depth 2
// MyCoroutine is now finished at depth 1
// MyCoroutine is now finished at depth 0

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)