In this article we are going to analyze Unity’s Update method which allows us to make changes over time in our projects.
A method is a function defined in a script that can be executed and performs the task we define inside it. To learn more about methods in programming you can read this article or watch a video that summarizes the information.
Before continuing I leave you an article with a downloadable prototype for Unity which has an experiment to understand the difference between the functions Update and FixedUpdate. This is the video using and explaining the prototype:
Update Method in Unity – MonoBehaviours
The Update function is defined in the MonoBehaviour class and will run automatically in each frame of the game if the MonoBehaviour is active.
When we create a new Script in Unity, by default we’ll get some code already written. In this code is defined a Programming Class that is called equal to the name that we gave to the Script and that extends or inherits its behavior of MonoBehaviour, this in simple terms means that our Script is in itself a MonoBehaviour or a particular case of MonoBehaviour.
MonoBehaviours can be added to the GameObjects that are in the hierarchy, this can be done from the inspector using the “Add Component” button or simply dragging the Script to the GameObject inspector.
Execution of the Update function
When running the game, Unity will automatically take all the MonoBehaviours in the scene and perform the Update methods before displaying each frame in the game. So we don’t have to do the execution of this method manually, the engine takes care of it.
This means that the Update function will run periodically while our game is running.
If our game runs at 60 FPS (frames per second) the Update function will run 60 times per second.
Conclusion – Update for changes in time
The Update method represents everything that is dynamic in our game, when we want to produce changes in time Update is one of the functions that we have to consider.
It is useful to understand the order of execution of the Start, Update and FixedUpdate methods since it allows us to identify different moments in the execution of the game.