Skip to content

Invoke vs. Coroutines

antfarmar edited this page Jul 22, 2017 · 1 revision

Invoke allows you to call a method by name (string), and a specified delay time to call it.

Coroutines can achieve the same effect with yield return new WaitForSeconds(delay). Coroutines also offer more flexibility than Invoke:

  • You can pass parameters to the coroutine, where local references are saved in the call stack.

Deactivated Game Objects: Difference between Invoke & Coroutines

  • Deactivating a game object does not stop the Invoke\Repeating().
    • To deactivate the whole game object: gameObject.SetActive(false)
  • Deactivating just the script component itself does not stop the Invoke\Repeating().
    • To deactivate just the script component: this.enabled = false
  • BUT NOTE THAT COROUTINES ARE DIFFERENT:
    • Ordinary coroutines do indeed get stopped when you deactivate the game object. However, if you deactivate only the script itself, then the coroutine does keep going.

For InvokeRepeating:

  1. disable whole game object - InvokeRepeating does NOT stop
  2. disable just the script component - InvokeRepeating does NOT stop

For Coroutine:

  1. disable whole game object - coroutine DOES stop.
  2. disable just the script component - coroutine does NOT stop.

In all four cases, the repeat is "eliminated", it is NOT paused. If you again SetActive(true), in all four cases, the repeat does NOT continue where it left off.

Clone this wiki locally