Say you have a function like this:

void SomeFunction(){
    //do stuff before SomeCoroutine starts...

    StartCoroutine(SomeCoroutine());

    //do stuff after SomeCoroutine finishes...
}

IEnumerator SomeCoroutine(){
    //do something...
}

One quick solution is to move the stuff you want to do after the coroutine finishes from SomeFunction to SomeCoroutine.

There is one more elegant and clean solution.

Essentially what we want to do is wait for the coroutine SomeCoroutine to finish. A function cannot do that. A coroutine though can. So, we will use another coroutine to solve this coroutine problem! I know it sounds more complicated, but bear with me. This is the code:

IEnumerator SomeFunctionTurnedCoroutine(){
    //do stuff before SomeCoroutine starts...

    yield return StartCoroutine(SomeCoroutine());

    //do stuff after SomeCoroutine finishes...
}

IEnumerator SomeCoroutine(){
    //do something...
}

And voila, the problem is solved. We turn SomeFunction into a coroutine and we wait for SomeCoroutine to finish (that’s what the yield command is basically for, “waiting”). When it does, we continue with the execution of the function as normal.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s