shafikshaon
shafikshaon

Reputation: 6404

What is MonoBehaviour in Unity 3D?

using UnityEngine;
using System.Collections;

public class VariablesAndFunctions : MonoBehaviour
{   
    int myInt = 5;
}

The full code is here Unity Official Tutorials

What is the purpose of MonoBehaviour

Upvotes: 15

Views: 43893

Answers (4)

Jackie Codes
Jackie Codes

Reputation: 3

Monobehavior is what most of your scripts inherit from,

if you go to the documentation Click here!

you will see a bunch of variables and methods you get from this Inheritance. such as:

  1. Public Methods
  2. Messages
  3. Properties
  4. Public Methods
  5. Static methods

The most commonly used method (its under message in the documentation but honestly its better to see it as a function) is Update , its the main game loop, the speed at which the update function is called is based on your fps. But the important thing to take away is that if you didn't inherit from monobehavior, you wouldn't have access to this game loop.

Another important function that you get from Monobehavior is Start, which is called once on a script, and it's called after awake, so if you want to set some variables up you can do it here.

The important thing to take is that if you made a simple C# class that inherits from nothing, you wouldn't have access to these methods discussed. Monobehavior gives you access to many functions that help you build your game.

There are other behaviors your scripts can inherit from like ScriptableObject and StateMachineBehaviour, which give you access to other methods, but Monobehavior is the most common behavior your scripts will inherit from.

It's also good to note that whenever you use Monobehavior, it comes with a transform, some other scripts (Scriptable objects) don't come with a transform. The transform is simply a position in your game/scene where the gameobject lies its an x,y,z coordinate with rotation and scale.

Upvotes: 0

Mustapha Othman
Mustapha Othman

Reputation: 105

While the following statement is correct,

  • "MonoBehaviour is the base class from which every Unity script derives" -

I honestly feel it can be misleading to beginners. The phrase - "every Unity script" - being the culprit.

It gives a beginner the notion that all scripts created in unity must extend Monobehaviour. Which is not the case. You can create scripts that house classes that extend the c# base object class. In doing so, your script is then categorised as not a Unity script but nothing stops it from interacting with other Unity scripts and vice versa.

Upvotes: 9

David
David

Reputation: 16277

MonoBehaviour is the base class from which every Unity script derives. It offers some life cycle functions that are easier for you to develop your app and game. A picture is worthy of thousands of words.

Source of the image: https://docs.unity3d.com/uploads/Main/monobehaviour_flowchart.svg

enter image description here

Upvotes: 38

TheLethalCoder
TheLethalCoder

Reputation: 6744

MonoBehaviour is another class that VariablesAndFunctions is inheriting from. This allows the inheritor to use the methods and variables of the other class providing they have the correct access level modifier set.

In the below example Class1 inherits from Base and so can use the protected method Method1

public class Base
{
    protected void Method1 { /*...*/ }
}

public class Class1 : Base
{
    public void Method2 { Method1(); }
}

Note in this particular example it would be better for Method1 to be marked as abstract or virtual so then Class1 can override it like so:

protected override Method1()
{
    //...

    base.Method1(); //Call the implementation of Method1 in Base here

    //...
}

In particular though MonoBehaviour is described as being:

MonoBehaviour is the base class from which every Unity script derives.

Therefore when doing scripting in unity, you use this base class to better control how things are accessed so you do not need to do it yourself.

Upvotes: 2

Related Questions