Lesson 9 – Update () and FixedUpdate()

Today we discuss another set of very common Methods() derived from Unity’s Monobehavior class. As mentioned before, MonoBehaviour is the base class from which every Unity script derives. AKA: the class that holds all of the Unity scripts we work with.

The methods in question are Update() and FixedUpdate(), and if you’ve been paying any attention at all you will have seen them being put to use in a script or two by now.

What is Update() and FixedUpdate()?

Unity Doc definitions of both:

Update(): Update is called every frame, if the MonoBehaviour is enabled.
Update is the most commonly used function to implement any kind of game behaviour.

FixedUpdate(): This function is called every fixed framerate frame, if the MonoBehaviour is enabled. FixedUpdate should be used instead of Update when dealing with Rigidbody. For example when adding a force to a rigidbody, you have to apply the force every fixed frame inside FixedUpdate instead of every frame inside Update.

Now, let’s speak on a more human level. Update() & FixedUpdate() are one of the most commonly used methods in Unity to implement behavior in a game. Since they are both consistently being called, it serves as a great area to implement code for game behavior.

However, there is a very important difference between both of them:
Update() is called every frame. Keep in mind that not all frames fall in the same time period. Some frames are longer then others depending on the current conditions of the game.
FixedUpdate() is called on a fixed timeline and will have the same time between calls. Immediately after a FixedUpdate() is called, any necessary physics calculations are made. This means that any physics based scripts you are going to write would need to be placed here to receive the most reliable and accurate physics behaviors.

What the hell is a frame??

If you are reading this, I want to desparately believe you are a gamer because like…what else would you be doing in this blog?… Any who, a frame in this context frames can be thought about in the same way you hear games being touted as 30FPS (Frames per second) or 60FPS. The frame rate in a video game reflects how often an image you see on the screen is refreshed to produce the image. This is the same case here.

So now that we have that thought in our heads, we can see how important methods like Update() & FixedUpdate() can be when we consider they are being called on every single frame.

I know you said there is a time difference between Update() & FixedUpdate(), but it can’t be that bad, right?

• Wrong! Turns out Update() almost never has a stable time between frames, so running any physics scripts in this method would cause you bugs and headaches one way or the other.

Consider the following script:

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

public class UpdateVsFixedUpdate : MonoBehaviour
	// Update is called once per frame
	void Update ()
        // print to the console the time lapse between Update() calls
		print("Update() Time: " + Time.deltaTime);

    // FixedUpdate is called once every fixed frame
	void FixedUpdate()
        // print to the console the time lapse between FixedUpdate() calls
		print ("FixedUpdate() Time: " + Time.deltaTime);

We have a simple script to grab the time on every call for the two methods. The image below proves to us that the descriptions of Update() & FixedUpdate() were accurate:


While Update() has captured a sporadic time length between calls, FixedUpdate() remained at a consistent 0.02.

PSA: One warning, from one noob to the next

Loops. You’re probably new to them like I am. Hell, we’re probably both new to every concept spoken of here. So, I hope you understand the concept of those methods running on every frame. It almost acts as a programming loop like while() or for(), right?

Well, if I can teach you anything, it’s the following:
BE CAREFUL WITH WRITING LOOPS SUCH AS while() and for() within the Update() & FixedUpdate() methods. Why the sense of urgency in my voice, you ask? We know a loop will run until a condition/boolean has been met, right? Now, imagine you call a loop on a METHOD LIKE Update() OR FixedUpdate(), which literally gets called every frame. They get called multiple times within a second. That means you have called this loop several dozen times as well. You can imagine what sorts of crashing/slowdowns one can experience if you do such a thing incorrectly. And that is my PSA.


These two methods are at the forefront of your code within Unity, so it is crucial you get familiar with what works best where. Seeing as I am focusing on 2D games without realistic physics, FixedUpdate() will get less attention from me then Update(), but it was beneficial to know the difference. As I keep learning, I am admiring even the smallest details in the Unity Engine.

Study Resources:
• https://docs.unity3d.com/Manual/TimeFrameManagement.html
• https://unity3d.com/learn/tutorials/topics/scripting/update-and-fixedupdate?playlist=17117


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s