-
Notifications
You must be signed in to change notification settings - Fork 15
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.
- Deactivating a game object does not stop the
Invoke\Repeating().- To deactivate the whole game object:
gameObject.SetActive(false)
- To deactivate the whole game object:
- Deactivating just the script component itself does not stop the Invoke\Repeating().
- To deactivate just the script component:
this.enabled = false
- To deactivate just the script component:
- 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:
- disable whole game object - InvokeRepeating does NOT stop
- disable just the script component - InvokeRepeating does NOT stop
For Coroutine:
- disable whole game object - coroutine DOES stop.
- 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.