What is a MonoBehaviour in Unity

Introduction

In this article we are going to talk about what MonoBehaviour is in Unity, first a practical approach to know in general terms what it is and what functions it fulfils concretely, then we analyze MonoBehaviour from the programming perspective.

What do we need to know about MonoBehaviour to work at Unity?

When we created a new script in Unity, next to the name we gave it appears the word “MonoBehaviour”, this in simple terms is telling us that the script we made will behave like a MonoBehaviour.

This means that Unity will be in charge of executing certain functions automatically. To understand the concept of function in programming I invite you to read this article.

For example the “Start” function will be executed when the GameObject to which we assign our Script appears in the scene (when we start the game or when we instantiate it).

Another function that is automatically executed is “Update”, which is executed before showing each frame of the game, so it is a function that allows us to update the state of the game and produce the dynamic.

These two functions are defined by default when we create a new script in Unity, but there are also other functions that we can define and will be executed automatically at a certain point in the life time of a MonoBehaviour. You can read more about the Update function here.

A useful function that is not defined by default is the “FixedUpdate” function, which is similar to Update, but is executed equispaced in time with a default frequency of 20 milliseconds. This allows us to achieve changes in the game that should occur regularly over time, for example the movement of objects.

Other useful functions are “Awake”, “LateUpdate”, “OnDestroy”, “OnEnable”, “OnDisable”,

What is MonoBehaviour from a programming perspective?

Mono Behaviour is a “Programming Class”, this means that it is a set of variables, objects and functions that perform a certain function.

To know more about what is a class and object in programming you can consult this article.

When we create a new Script in Unity, what we do in the background is to create a new programming class that will have the same name that we gave the Script and that will also extend from MonoBehaviour, that is to say that it will inherit its behaviour from the MonoBehaviour class. That’s why the “Start”, “Update” or ”FixedUpdate” methods are automatically executed when the game starts running.

Exit mobile version
Secured By miniOrange