Exit Strategy

I added a check for the player tag in the OnTriggerEnter2D()

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.CompareTag("Player"))
    {
        StartCoroutine("LoadNextLevel");
    }
}

I assume Rick used a coroutine just to demonstrate the use of coroutines rather than the more straightforward Invoke delay method? Are there any advantages in using coroutines rather than invoke in this context?

Hi RedSandman,

A coroutine is more flexible than a method we call via Invoke. In our coroutine, we are able to pause the execution and continue it at a later juncture.

If you ‘just’ want to call a method with a delay, you could use Invoke. However, everybody who develops games with Unity should also know about Unity’s coroutines.


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms