Coroutines vs Invoke

Hey everyone! Loving the TileVania section so far. Quick question in regards to using Coroutines vs Invoke.

Within both Unity courses, I’ve noticed how we’ve used both in the case of calling a method after a delay. Is there a reason for using one over the other for this purpose, or more generally, when is it more appropriate to use Coroutines over Invoke, or vice versa?

Thanks!

Hi Steven,
Once you know how to use coroutines it’s pretty much always a better idea to use them over invoke.

Behind the scenes, invoke is just a coroutine so they function in exactly the same way. However, since invoke uses a string reference it’s more prone to error. If you spell something wrong, or you change the method name you won’t get any errors and will end up spending ages trying to figure out why your code isn’t working the way it should.
Coroutines on the other hand need to be given a valid method name, so it’s a lot more resilient and will throw errors if you do something wrong.

Invoke is a good primer into the idea of how coroutines work, which is why it is in the earlier sections of the course, but you really should try to avoid using it once coroutines make sense to you.

Hope that helps.

4 Likes

Ah, good to know. Thanks for the answer, Gary!

1 Like

Privacy & Terms