About UPDATE method in Unity

Introduction

In this arti­cle we ana­lyze the Update func­tion from the Uni­ty engine, that func­tion is defined inside the MonoBe­hav­iour objects and it's auto­mat­i­cal­ly exe­cut­ed every frame by Uni­ty on every MonoBe­hav­iour that is enabled.

So the update method allows us to make changes over time in our game or appli­ca­tion made in Uni­ty. For exam­ple we can move objects or grad­u­al­ly change a col­or inside an Update.

ABOUT THESE VIDEOS

Here you have two videos about the UPDATE FUNCTION in Uni­ty. In the video on the left we see a pro­to­type made in Uni­ty to point the dif­fer­ences between the Update and FixedUp­date func­tions in Uni­ty. You can down­load the Uni­ty Pack­age in this arti­cle. In the video from the right we see how to grad­u­al­ly change vari­ables in Uni­ty, mak­ing incre­men­tal changes inside the UPDATE or the FIXEDUPDATE method in Unity.

HERE MY UNITY PLAYLIST
👇🏽

Update Method in Unity — MonoBehaviours

The Update func­tion is defined in the MonoBe­hav­iour class and will run auto­mat­i­cal­ly in each frame of the game if the MonoBe­hav­iour is active.

When we cre­ate a new Script in Uni­ty, by default we'll get some code already writ­ten. In this code is defined a Pro­gram­ming Class that is called equal to the name that we gave to the Script and that extends or inher­its its behav­ior of MonoBe­hav­iour, this in sim­ple terms means that our Script is in itself a MonoBe­hav­iour or a par­tic­u­lar case of MonoBehaviour.

MonoBe­hav­iours can be added to the GameOb­jects that are in the hier­ar­chy, this can be done from the inspec­tor using the "Add Com­po­nent" but­ton or sim­ply drag­ging the Script to the GameOb­ject inspector.

Execution of the Update function

When run­ning the game, Uni­ty will auto­mat­i­cal­ly take all the MonoBe­hav­iours in the scene and per­form the Update meth­ods before dis­play­ing each frame in the game. So we don't have to do the exe­cu­tion of this method man­u­al­ly, the engine takes care of it.

This means that the Update func­tion will run peri­od­i­cal­ly while our game is running.

If our game runs at 60 FPS (frames per sec­ond) the Update func­tion will run 60 times per second.

Conclusion — Update for changes in time

The Update method rep­re­sents every­thing that is dynam­ic in our game, when we want to pro­duce changes in time Update is one of the func­tions that we have to consider.

It is use­ful to under­stand the order of exe­cu­tion of the Start, Update and FixedUp­date meth­ods since it allows us to iden­ti­fy dif­fer­ent moments in the exe­cu­tion of the game.

Scroll to Top
Secured By miniOrange