In this article we analyze the Update function from the Unity engine, that function is defined inside the MonoBehaviour objects and it’s automatically executed every frame by Unity on every MonoBehaviour that is enabled.
So the update method allows us to make changes over time in our game or application made in Unity. For example we can move objects or gradually change a color inside an Update.
ABOUT THESE VIDEOS
Here you have two videos about the UPDATE FUNCTION in Unity. In the video on the left we see a prototype made in Unity to point the differences between the Update and FixedUpdate functions in Unity.
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.