Nested coroutines

What would be the difference of doing this from inside a CoRoutine? (LerpSize() is also a CoRoutine)

yield return new LerpSize (targetSize, lerpDuration); 

yield return  LerpSize (targetSize, lerpDuration); 

StartCoroutine( yield return new LerpSize (targetSize, lerpDuration) );

There is not difference since non of them would work :wink:

You have to do it like this:

    yield return StartCoroutine(LerpSize (targetSize, lerpDuration));

StartCoroutine returns a “Coroutine” object. This object can be yield inside a coroutine to wait for the inner coroutine. Anyways the “nested” coroutine will be a completely seperate coroutine. It you yield a nested coroutine, the outer coroutine just waits for the inner coroutine to finish.

You can use the statement

yield return new LerpSize (targetSize, lerpDuration);

from within a different coroutine function to delay execution of code beyond that statement until the statement is finished. In this case LerpSize is a class. This depends on whether inheriting from YieldInstruction (which I haven’t tested to be actually possible!) and LerpSize in this case would be a class with the following constructor:

public class LerpSize : YieldInstruction
{
    LerpSize(float targetSize, float lerpDuration) : base()
    { 
        //The lerp sizing function here
    }
}

You can use

StartCoroutine(LerpSize (targetSize, lerpDuration));

to make the method LerpSize(float, float) run as a coroutine. This means it will run independent of the main application loop and you can yield (wait) inside the method.

 IEnumerator LerpSize(float targetSize, float lerpDuration)
        {
            //Lerp code here
        }