- Home /
How exactly do Monobehaviour functions work?
So I've used functions like Awake, FixedUpdate, OnTriggerEnter and whatnot for some time now, but I'm not sure I fully understand how those functions work on a larger scale. I notice that they don't need to be tagged as "new" or "override". And it will compile and function properly regardless of whether one of these functions is actually implemented in a custom script.
What's going on with these functions, exactly? How do these built-in functions fit into the larger picture of object-oriented programming?
As far as I can tell, they don't fit in well with the concepts of C# because they are specific to Unity.
For instance, I've frequently heard monobehaviours without an Update method are faster than monobehaviours with empty Update methods. So it seems like monobehaviours without an Update function never have a Update called. This pretty much requires that $$anonymous$$onobehaviour functions behavior to be defined by Unity rather than something general to C#.
Answer by nisovin · Aug 04, 2015 at 12:32 AM
From what I understand, those are called using the message system in Unity. For example, by using GameObject.SendMessage or the other related methods (BroadcastMessage, SendMessageUpwards). I don't know exactly what the internal implementation of it is, but I've always assumed it's using some kind of reflection.
Some great answers and comments from everyone. I also had a feeling it was the messaging system. I know a bit about reflection and invoking methods based on name, but I've heard reflection can be really slow.
I think for my own purposes I'll probably use virtual and override functions for the time being.
Answer by ninjaboynaru · Aug 04, 2015 at 12:23 AM
I'm not 100% sure that this is how they work or that I fully understand your question, but this is how I would do it and it seems to be the most logical..
When an event happens in the Unity world, ie.(the game starts), Unity goes through each script in the game that is attached to a game object and calls the corresponding function for that event.
In this case, the corresponding function for the game/scene starting would be Awake()
or Start()
. The Update() function would correspond to the passing of a single frame.
Your answer
Follow this Question
Related Questions
The name '' does not exist in the current context 2 Answers
Creating a pointer variable to a GameObject inside a class that does not extend MonoBehavior? [C#] 2 Answers
Classic Class in C#? 3 Answers
Issues Inheriting classes and monoBehaviour 1 Answer
Referencing NON-monobehavior scripts - getting an error :/ 1 Answer