Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are the MonoBehaviour methods not implemented for overriding?

Tags:

c#

unity3d

In Unity3d you have the MonoBehaviour class, which is the normal base class for all scripts. When implementing a script, one has to implement the methods such as Awake() or Start() or Update().

However, These methods are not implemented as virtual or abstract in the MonoBehaviour class so that one could easily override them; in fact they are not implemented at all. The method one writes at the Moment is a new method that the class did not have beforehand.

Why are these methods not implemented as virtual or abstract in the base class (MonoBehaviour)?

like image 786
HerpDerpington Avatar asked Sep 12 '15 10:09

HerpDerpington


People also ask

What does MonoBehaviour mean?

Description. MonoBehaviour is the base class from which every Unity script derives. When you use C#, you must explicitly derive from MonoBehaviour. When you use UnityScript (a type of JavaScript), you do not have to explicitly derive from MonoBehaviour.

Is MonoBehaviour a component?

Yes. MonoBehaviour inherits directly from Behaviour which then inherits from Component . Collider inherits from Component directly. Then non base colliders such as BoxCollider and SphereCollider inherits from that Collider .

How do I add MonoBehaviour?

MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all.

What is inside MonoBehaviour?

MonoBehaviour is the base class that every Unity script has to be inherited. A Unity script, that is derived from a MonoBehaviour, serves a bunch of predefined functions(Awake, Start, Update, OnTriggerEnter, etc.) that are executed when an event occurs.


1 Answers

If you check the documentation you will see that all those 'functions' are listed in the 'Messages' section; MonoBehaviour Doc.

So that means that 'functions' (like Start, OnEnable or Awake), they are not implemented as methods but as messages.

Now, MonoBehaviour inherits from Behaviour, which inherits from Component which has the SendMessage method. That method basically calls a message/method with the option to report an error if the message/method exists or not. The parameter of the name message/method is a string, /~~so they are using reflection there.~~/ Check Update!!

Seems like Unity behind the scenes determines if those messages were implemented to see if it has to call them or not. According to this UnityAnswer:

Unity is mainly written in c++ and therefore most magic happens in native code. Unity uses reflection to determine, after your scripts have been compiled, what of those "events" you've implemented and remember that for this class. Unity only calls Update / LateUpdate / OnGUI when it has been implemented.

So, the short answer is, they are not 'real' methods that you have to override, they are messages that are called only if they were implemented.


Update; As @Raining noted, I was wrong in the way that Unity obtains the messages to call. Unity doesn't use reflection to do this. According this 1k-update-calls (also provided by @Rainin) scripts are inspected to check if they contain some of those 'magic methods'. If so, they are added to lists that will be executed accordingly.

like image 54
mayo Avatar answered Nov 15 '22 20:11

mayo