15
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

asked Jan 13, 2017 at 9:13
4
  • 2
    Its a class which provides entrymethods like Start(), Update() and things so you dont have to worry about such things. Commented Jan 13, 2017 at 9:15
  • Ok understood. Thanks :) Commented Jan 13, 2017 at 9:17
  • 1
    You should read some C# basics Commented Jan 13, 2017 at 9:17
  • 1
    You really should try a search engine before resorting to asking for help. Google "What is a unity3d monobehaviour" - first hit is the official documentation explaining it. Commented Jan 20, 2017 at 9:34

4 Answers 4

39

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

answered Jan 13, 2017 at 9:19
Sign up to request clarification or add additional context in comments.

1 Comment

Just to add to this, it also allows to be treated in the Editor as a component so you can drag it into the inspector.
9

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.

answered Apr 11, 2020 at 9:17

2 Comments

That's a valid and interesting point but, to me, it feels like the original (and surely, too vague) question was more about what the declaration of a superclass actually means in the first place. Otherwise, my guess would be that they're asking about what the MonoBehaviour class is and does. Your answer opens a different discussion: one about architecture.
Thank you for clarifying this- I thought exactly this: that monobehaviour is c#'s version of "all Java classes derive from Object"
2

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.

answered Jan 13, 2017 at 9:17

1 Comment

@ShafikShaon It means that MonoBehaviour is a base class and VariablesAndFunctions is a class that is inheriting from it
0

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.

answered Dec 19, 2021 at 13:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.