Unity Lifecycle: Update Vs FixedUpdate Vs LateUpdate

Unity Lifecycle: Update Vs FixedUpdate Vs LateUpdate

Choosing Where to Update

Introduction

When creating new C# scripts within Unity you will notice that the script is generated with two default methods, Start and Update. These methods are part of the script lifecycle and are called in a predetermined order. In this post we will discuss the three update methods.

About

  • Subject: Unity
  • Objective: To discuss the difference between Unity lifecycle methods.
  • Development Time: 8 min

Getting Started

FixedUpdate

The Unity description for FixedUpdate

Frame-rate independent MonoBehaviour.FixedUpdate message for physics calculations.

FixedUpdate is specifically designed for physics calculations. If you are using a rigidbody to control movement or add additional physics manipulations, you will want to place that code here. The default execution time for FixedUpdate is 0.02 seconds or 50 calls per second. This value can be changed in the settings at Edit > Project Settings > Time > Fixed Timestep

This is also the same time you are accessing when you call Time.fixedDeltaTime.

The consistent rate at which this function is called is the reason you want to place physics manipulations here instead of Update which we will talk about next.

Update

The Unity description for Update.

Update is called every frame, if the MonoBehaviour is enabled.

A frame is a screen update, think of it as a frame in a movie reel or a single page in a flip book. With each frame update the screen is redrawn to reflect changes in the game. The update method is where the majority of your code will go that affects the world outside of physics.

LateUpdate

The Unity description for LateUpdate.

LateUpdate is called every frame, if the Behavior is enabled.

This is called at the end of each frame update. This ensures that code inside of Update, changes in states, and animations have had time to occur. This method gives you a layer of control where you can guarantee that code here will run after the others. Unity gives the recommendation that camera movement should be implemented here. This is because if an object is moved through code or animation the camera will know its final location for the frame and keeps your objects from moving after the camera position has updated.

Testing

We can demonstrate the frequency of the update methods by using the script given to us by Unity here.

We will make the following modifications to add LateUpdate to the GUI.

Add these fields.

    
    private float lateUpdateCount = 0;
    private float updateLateUpdateCountPerSecond;

Add this to the OnGUI method.

        
    GUI.Label(new Rect(100, 200, 200, 50), "LateUpdate: " +  
              updateLateUpdateCountPerSecond.ToString(), fontSize);

And at last, add this to the Loop method.

    
    updateLateUpdateCountPerSecond = lateUpdateCount;
    lateUpdateCount = 0;

Your full script should look like this:


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

// GameObject.FixedUpdate example.
//
// Measure frame rate comparing FixedUpdate against Update.
// Show the rates every second.

public class ExampleScript : MonoBehaviour
{
    private float updateCount = 0;
    private float fixedUpdateCount = 0;
    private float updateUpdateCountPerSecond;
    private float updateFixedUpdateCountPerSecond;

    private float lateUpdateCount = 0;
    private float updateLateUpdateCountPerSecond;

    void Awake()
    {
        // Uncommenting this will cause framerate to drop to 10 frames per second.
        // This will mean that FixedUpdate is called more often than Update.
        Application.targetFrameRate = -1;
        StartCoroutine(Loop());
    }

    // Increase the number of calls to Update.
    void Update()
    {
        updateCount += 1;
    }

    // Increase the number of calls to FixedUpdate.
    void FixedUpdate()
    {
        fixedUpdateCount += 1;
    }

    void LateUpdate()
    {
        lateUpdateCount += 1;
    }

    // Show the number of calls to both messages.
    void OnGUI()
    {
        GUIStyle fontSize = new GUIStyle(GUI.skin.GetStyle("label"));
        fontSize.fontSize = 24;
        GUI.Label(new Rect(100, 100, 200, 50), "Update: " + updateUpdateCountPerSecond.ToString(), fontSize);
        GUI.Label(new Rect(100, 150, 200, 50), "FixedUpdate: " + updateFixedUpdateCountPerSecond.ToString(), fontSize);
        GUI.Label(new Rect(100, 200, 200, 50), "LateUpdate: " + updateLateUpdateCountPerSecond.ToString(), fontSize);
    }

    // Update both CountsPerSecond values every second.
    IEnumerator Loop()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            updateUpdateCountPerSecond = updateCount;
            updateFixedUpdateCountPerSecond = fixedUpdateCount;

            updateLateUpdateCountPerSecond = lateUpdateCount;
            lateUpdateCount = 0;

            updateCount = 0;
            fixedUpdateCount = 0;
            
        }
    }
}

Attach the script to any GameObject in your scene and hit play.

In the top left corner, you will see three numbers appear. These numbers represent the number of times per second the method is called. As you can see, the Update and LateUpdate methods are called at the same rate but fluctuate up and down while the FixedUpdate method stays at a consistent 50 updates per second.

Thank you for stopping by. Stick around and check out more of our tutorials or posts like our piece on Using Unity Analytics. Also, leave a comment telling us what you liked or did not like about the tutorial. Was it easy to follow along? What do you want to learn next? As always check out some of our published apps below.

Leave a Comment

Your email address will not be published.