You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

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

Ben Liu
Ben Liu

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:
  • 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 destroyed. 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:

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp