Mastering Unity Callback Methods: Understanding Their Order of Execution and Importance

Mastering Unity Callback Methods: Understanding Their Order of Execution and Importance

In this article, I share my experience of using Unity methods, their order of execution, and why they are important for every Unity developer to understand. I have included the most commonly used callback methods and explained their purpose.

If you notice anything missing from my explanation, please let me know.

Below are some of the most important Unity methods that I have used listed in order of execution, along with examples:


Awake: Called only once in a lifetime, even if the script is disabled. It is used for initialization.

Example

private Rigidbody rb;

private void Awake() 
{
    rb = GetComponent<Rigidbody>();
}        

OnEnable: Called when the object or the script is enabled. Note that both the object and script must be enabled for OnEnable to work.

Example

private void OnEnable()
{
    // Usually used for subscribing to events
    GameManager.Instance.OnGameOver += GameOver; 
}        

Start: Called once in a lifetime, but after Awake. It is used for setting values.

Example

private void Start()
{ 
    rb.useGravity = true;
}        

FixedUpdate: Called after every fixed interval (default value is 0.02 seconds, so 1 second/0.02 seconds = 50 times per second). It is used for physics-related stuff since you want consistent behavior on every device.

Example

private void FixedUpdate()
{ 
    rb.velocity = Vector3.forward * 12;
    // Since it's called at a fixed interval, there is no need for Time.DeltaTime.
}        

OnTriggerEnter: Called when the object passes through another object or when the other collider is set to OnTriggerEnter. Both colliders can have OnTriggerEnter and will also call this method. Note that one of them must have a Rigidbody.

Example

private void OnTriggerEnter(Collider other)
{ 
    if(other.gameObject.CompareTag("Player")) {
        // Set the coin to false.
        gameObject.SetActive(false);
    }
}        

OnCollisionEnter: Called when two game objects having colliders collide with each other. Note that one of them must have a Rigidbody.

Example

private void OnCollisionEnter(Collision collision) 
{
    // If the player collides with an enemy, damage the player.
    DamageThePlayer(10);
}        

Update: Called as many times as your device is capable of rendering frames, and the delay between each update call can vary. Update is usually used for getting input.

Time.deltaTime is the delay between each Update call and it can vary depending on the device's capability to render frames. On devices that can render more frames per second, the value of Time.deltaTime is smaller, while on devices that can render fewer frames per second, the value is larger. For example, if a device renders 30 frames in one second, then Time.deltaTime is 0.03, while if it renders 60 frames in one second, then Time.deltaTime is 0.01. It's important to note that the value of Time.deltaTime can change if VSync is not turned on.

Example

private void Update()
{ 
    if(Input.GetKeyDown(KeyCode.Space)) {
        Jump(); // If you want consistent behavior, you should multiply with Time.DeltaTime.
        // Example:
        transform.position += 12 * Time.deltaTime * Vector3.forward; // 12 is the speed here, you can create a field for it instead of hardcoding.
        // Time.DeltaTime values will adjust based on the device.
    }
}        

LateUpdate: Called after Update. It is used to add a delay, for example, if the player is moving and the camera is following.

Example

private void LateUpdate() 
{
    // Use LateUpdate so that if the player makes a movement, the camera can follow smoothly.
}        

OnDisable: Similar to OnEnable, but called when the object or script is disabled.

private void OnDisable(
{
    // Usually used for Unsubscribing from events
    GameManager.Instance.OnGameOver -= GameOver; 
}        

OnDestroy: Called when the object on which the script is attached is destroyed and removed from the heap.


Reference Link

To view or add a comment, sign in

More articles by Adeel Bashir

Explore content categories