MonoBehaviour.Update() in Unity
In this article, we will discuss the Update method in Unity which allows us to make changes in the projects over time.
MonoBehaviour.Update method in Unity
The update method is called once per frame depending on the game frame rate if the MonoBehaviour is enabled. Frame refresh rate is measured in FPS (Frames Per Second).
The update method is best for any non-physics-based updates, receiving user input, etc.
For example, if we want to check if a game object becomes visible or not from a specific camera, we need to do it in the update method. We will continuously check if the object is in the camera range or not. This task will be done in every frame while playing the game.
Execution of the Update() method
While running the game, Unity will perform the Update methods before each and every frame. Once the game is running update method will automatically execute itself.
This means that the Update function will run continuously while our game is in play. So if we need to run any code continuously while the game is playing, then we need to put the code in the update function.
If our Unity game runs at 60 FPS (frames per second), then the Update function will run 60 times per second.
Syntax:
Below is given the syntax of the Unity update() method:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class UpdateMethod : MonoBehaviour { // Update is called once per frame void Update() { Debug.Log("Update Method"); } }
Output:
The update method is called in each and every frame.
Leave a Reply