In Unity, what exactly happens when I implement Update (), and other messages from MonoBehaviour

advertisements

I'm pretty new to C#, coming from a strong background in Java, python, and some "web" stuff. So I'm probably just missing some knowledge here, that hopefully somebody can fill in for me.

Going through the Unity Scripting API, I noticed a lot of methods, listed under "Messages", which is my first point of confusion. What exactly is the difference between a "Message" and a "Method", to me they seem like they are the same thing, but the terminology is throwing me off.

My second point of confusion, is that when I implement a "Message" like Update() in my derived classes, I don't have to use the override keyword. Which is bugging me, because wouldn't I just be hiding it? In which case, wouldn't the update loop just know about MonoBehaviour and not my derived class, and call Update() in MonoBehaviour, instead of my class? This is throwing me for a loop. I'm amusing this is something going on behind the scenes?

I've been getting along really well with Unity, and C#, this is just a sticking point for me, where I feel like I'm missing some knowledge.


One thing to remember is that the Unity team didn't necessarily design their scripting API (including the MonoBehaviour class) to promote good software development principles; they designed it for accessibility and ease of use (especially for non-programmers). This is slowly changing, but there's still a lot of inertia toward "what's easy" instead of "what's right."

That said, let's get to answering your question. Regarding "messages," check out what the docs say about Component.SendMessage:

Calls the method named methodName on every MonoBehaviour in this game object.

When you look at the documentation for, say, Collider and see the methods listed under the Messages section, that's an indication that if you implement those methods, they will be called (via SendMessage) in response to some event or action (OnCollisionEnter, for example, "is called when this collider/rigidbody has begun touching another rigidbody/collider."). I might have implemented them as event delegates, but the Unity team chose to implement their own messaging system using strings, again, in the name of "ease of use."

To that same effect, if you walk up the inheritance chain for a MonoBehaviour class, you'll notice that nowhere in the chain do you actually find declared methods named Update or Start or the other lifecycle events of your scripts. Unity is treating these as magic methods and calling them (again via SendMessage, it appears) if they exist in the implementing class. That's why it doesn't matter if your Update method is public or protected or even private... it'll still get called.