In this article we will analyze the execution of a simple program to understand what are the differences between Update and FixedUpdate in Unity.
To see these differences I set up a simple Unity scene with two cubes and some programming scripts, you can download the Unity package in this article.
Downloadable Unity Package
Below you can download a Unity package to import and see how two objects behave with a translation applied in the Update method in one case and in the FixedUpdate method in the other.
What are the Update and FixedUpdate methods
To begin with, the Update and FixedUpdate methods are functions that belong to the MonoBehaviour class and will be executed automatically and cyclically when we place those Scripts in the hierarchy.
Summary of the differences between Update and FixedUpdate in Unity
1. The number of Update runs per second is variable, while the number of FixedUpdate runs per second is fixed.
2. In general, you have no control over the execution of the update, it will depend on the performance of the individual computers. For FixedUpdate we can modify the Time Step in Project Settings > Time, by default is 0.02 seconds (20 milliseconds), so we know exactly how many times this function is executed.
3. Both functions are used to make changes in time, from the above it can be concluded that Update is used for the logical part and FixedUpdate for the physical part, movements and animations, i.e. actions that must change regularly in time.
How to see the differences between Update and FixedUpdate using the package to download
When you import the package you will have a folder that contains all the necessary files, what you have to do is open the scene that is inside the folder, called “UpdateFixedUpdate”.
Inside the scene we have a frame per second counter to know the refresh rate of the game at all times.
We have two Cubes called Update and FixedUpdate, both have assigned a script called “UpdateFixedUpdate” in which is defined a speed for the cube movement and a logical variable that will determine if the cube movement is done in the Update function or in the FixedUpdate function.
Then there is another GameObject called Saturation that has assigned a Script called “CreateObjectsForever”, when this Script is activated it will start instantiating cubes as children of the Saturation GameObject in an uninterrupted way, which will cause little by little a processing overload and with a little analysis it will allow us to understand the differences between Update and FixedUpdate in Unity.
Procedure for Analysis
First Test without Overload
Initially we make sure that the GameObject Saturation is disabled or your “CreateObjectsForever” script is disabled, so that initially no saturation occurs in the game.
We make sure that both objects have the same speed in their Script UpdateFixedUpdate and that in the Update object the box “Use Fixed Update” is unchecked, while in the FixedUpdate object, that box is checked.
We enter the game mode and observe how the cubes behave. In my case we can see that the cube that moves using the Update method moves faster than the cube that moves in the FixedUpdate method. The cube that moves with Update does so at more than twice the speed and sometimes moves faster and sometimes slower, while the other cube moves regularly over time.
Second Test with Overload
Now we activate the Saturation object (or its script), to activate the instantiation of the objects.
When entering the Game Mode, you will notice that the game’s FPS begin to gradually decrease. This change can be even more drastic if we select the Saturation object in the hierarchy and if we are looking at it closely in the Editor tab. But the most important thing that we notice is that the speed of the cube that moves with Update starts to decrease, the more the overload, the slower the cube moves, until in a moment it is overtaken by the cube that moves with FixedUpdate.
This indicates that the higher the processing saturation, the Update function is executed less times per second, while the FixedUpdate function is executed regularly in time and, even though the game starts to have lag, the movements will be proportional.