C#

Methods in MonoBehaviour and execution order

ยท 2 min read
Methods in MonoBehaviour and execution order
MonoBehaviour method running order

In Unity, MonoBehaviour is a class that you can use to extend the functionality of a game object. When you attach a script derived from MonoBehaviour to a game object, several methods in the script are automatically executed by Unity in a specific order.

Here's the order of execution for some of the most commonly used MonoBehaviour methods:

  1. Awake: This method is called when the script instance is being loaded. It's typically used to initialize variables or perform other setup operations before the game starts.
  2. Start: This method is called on the frame when a script is enabled, after all the Awake methods have been called. It's typically used to initialize variables or perform other setup operations before the first update.
  3. Update: This method is called once per frame. You can use this method to update the game state, perform physics calculations, and perform other per-frame operations.
  4. FixedUpdate: This method is called at a fixed interval. You can use this method to perform physics calculations and other operations that need to occur at a fixed rate.
  5. LateUpdate: This method is called after all other update methods have been called. You can use this method to perform operations that should occur after all other updates have been processed.
  6. OnDestroy: This method is called when the script instance is being destroyed. You can use this method to perform cleanup operations and free up resources when a game object is being destroyed.

Above is just some well-known/frequent methods used in development, there are more methods can be found here:

private void Awake() { /* Called when the script is being loaded */ }
private void OnEnable() { /* Called every time the object is enabled */ }
private void Start() { /* Called on the frame when the script is enabled */ }
private void Update() { /* Called once per frame */ }
private void LateUpdate() { /* Called every frame after Update */ }
private void OnBecameVisible() { /* Called when the renderer is visible by any Camera */ }
private void OnBecameInvisible() { /* Called when the renderer is no longer visible by any Camera */ }
private void OnDrawGizmos() { /* Allows you to draw Gizmos in the Scene View */ }
private void OnGUI() { /* Called multiple times per frame in response to GUI events */ }
private void OnApplicationPause() { /* Called at the end of a frame when a pause is detected */ }
private void OnDisable() { /* Called every time the object is disabled */ }
private void OnDestroy() { /* Only called on previously active GameObjects that have been destroyed */ }