Unity: Are you familiar with Awake(), OnEnable(), Start(), Update(),FixedUpdate() and OnDestroy()

Unity: Are you familiar with Awake(), OnEnable(), Start(), Update(),FixedUpdate() and OnDestory()

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:

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:
    • The movement of physics object, which game object has a Rigidbody

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SignScript : MonoBehaviour {
    
    private void OnDestroy() {
        Debug.Log("I am destroied"); // you can put your explosion animation here
    }
}

Reference

Suggest you to have a good read on those following docs:

comments powered by Disqus