The very first thing that anyone notices when starting with scripting in Unity is the Unity Start and the Unity Update function. They are added by default in all the new scripts and there is also an explanation of what these functions do. But, there is confusion among new developers in how the Start and the Update function is used. In this post, we will cover the basics of what the Unity Update function does and also go through the different variants of the Update function like Late Update and Fixed update functions.
What is Update in Unity?
The update is a function in Unity that is called for every frame. Any script inside the Update function is executed once every frame. For example, If you add 1 to a variable in the update function and run the game at 30 FPS then, the variable value would increase by 30 every second. You must remember that the script and the gameobject must be enabled for the Update function to be called.
Variants of Unity Update Function
Late update function is similar to the Unity Update function. The only difference between the Update and the Late Update function is that the Late Update is called after all the Update Functions are called. You can use it in places where you want to do something every time after the Update function is executed.
For example, if you want the camera to follow the player and if you add the camera follow script and player movement script in the Update function then the player’s movement will not be followed by the camera in the same Update. But, if you add the camera follow script in the Late Update then the camera will move to the new position after the player has moved.
Physics has its own play in all aspects of game development. Even the Unity Update function has a physics variant called Fixed Update. The Fixed Update is called depending on the Fixed timestep variable. You can set from edit>settings>Time>Fixed Timestep. Fixed Update is called based on this variable and does not depend on the frames per second. Mostly Fixed Update is used when some physics movements are needed in the game. It is not like you cannot do the physics calculation in the Update function but the problem the time between each Update function call will vary based on the device FPS.
Is the Update Function Useful?
YES, very much. The Update is the most used function in Unity. I don’t remember making even a single game without the Update functions. Even though the Update functions are more performance-hungry they are just not neglectable while coding for your game. But you are always free to decide on what to put in the Update function and what not to.
Can I delete Update function from Unity script?
Yes, there are many scenarios where you would not need a loop to be executed. You can very well delete the Update function from the script if you don’t need it. For example, when you need to reduce the health of the player when he collides against the obstacle, you don’t need the update function you can use the OncollissionEnter function for this.
When to avoid Update function
Even though Update is the most used function in Unity scripts. It’s better to avoid it whenever possible. Like if you have a condition inside the Update that doesn’t need to be checked every frame you can put the script outside the Update function and call it when required. For example, if you need to increase the ammo count when the player picks up the ammo pack you don’t have to put this condition inside the Update. If you put it inside the Update with an if statement then the Update checks for the condition in every frame which is not required.
Hope I have cleared all your doubts on the Unity Update function. If not leave it in the comments below and I will update the article. If you like the article please share it with your friends and community. You can also join our Facebook group to make your first game in 10 days.