Unity: Are you familiar with Awake(), OnEnable(), Start(), Update(),FixedUpdate() and OnDestroy()
There are so many articles on explaining the difference between the following built-in methods in Unity:
- Awake()
- OnEnable()
- Start()
- FixedUpdate()
- Update()
- OnDestroy()
Those methods are quite frequently used in Unity everyday.
TL;DR
Short version of the execution order:
Difference between Awake(), OnEnable() and Start():
Awake()
: Is called only once in lifetime. No matter whether the script is enabled or not.OnEnable()
: Is called when game object's status from "disable" to "enable".Start()
: Called once in lifetime after Awake() but before Update(), also need this script to be enabled.
Difference between Update() and FixedUpdate():
Update():
Once per frame but not called on a regular timeline. High-end machine means more frames per second, which Update() is called more often than a normal machine. Some typical use scenarios are:
- The movement of non-physics object
- Detection of the input, such as mouse click, keyboard type, etc
FixedUpdate()
: Called on a regular timeline and have the same intervals between calls. Some typical use scenarios are:
There is a video made by Unity officially explaining the difference: https://learn.unity.com/tutorial/update-and-fixedupdate
OnDestroy(): Will be called when the game object is destroied. For example, when the character died, and I want some explosion effects. For example:
Reference
Suggest you to have a good read on those following docs:
- https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.htm
- https://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html
- https://docs.unity3d.com/Manual/ExecutionOrder.html
Originally published at https://needone.app on June 15, 2020.