What's the difference between a method and a message?
In the Unity documentation for C#, OnTriggerEnter2D is categorized as a message derived from MonoBehaviour. But to me it looks the same as a method. What is the difference between a method and a message and when would I use one vs. the other?
I am new to coding, and C#, and Unity, and I am working with the following script which is from a tutorial I am following. I guess my question boils down to trying to understand how calling a method and creating/defining a new method are different.
public class LoseCollider : MonoBehaviour {
private LevelManager levelManager;
void OnTriggerEnter2D(Collider2D trigger) {
levelManager = GameObject.FindObjectOfType<LevelManager>();
levelManager.LoadLevel ("Lose Screen");
}
My understanding is that a pre-existing method can be called from a script, and usually I would write the method name and insert some parameter, if applicable, and that's it.
I also know that I can 1. create and define a new method (using brackets to define what it does), and 2. call it elsewhere in the script.
My confusion is that OnTriggerEnter2D looks like a method, but in the script above it seems like I am both calling it (which makes sense because it's been defined by Unity) AND defining what that method does. My impression was that you would only define what a method does when you are creating a new one.
So that leaves me confused as to how can I define a method (put stuff between the brackets/curly braces) that has already been defined by Unity, which is what it seems the script is doing? So that's why I thought that maybe the answer has to do with the fact that OnTriggerEnter2D is a message, not a method. Do messages work differently than methods?
Please add a link, I wouldn't want to comment on what someone else's writing means without seeing what they've actually written.
Hi, here is the link: http://docs.unity3d.com/ScriptReference/Collider2D.html
The page is for Collider2D, and in the section called $$anonymous$$essages it lists OnTriggerEnter2D. "Sent when another object enters a trigger collider attached to this object (2D physics only)."
I don't see the word "message" anywhere? http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter2D.html
It does have it in the 3D version http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter.html
And on that page I$$anonymous$$O it's just a bit of sloppy writing, saying "This message..." rather than "The message that triggers this handler..." or something like that.
Answer by LessThanEpic · Jan 07, 2016 at 10:36 PM
It's both. It might make a little more sense if you check out the SendMessage documentation. OnTriggerEnter2D isn't defined in the base MonoBehaviour class, so you don't have to specify override in your derived class. Instead Unity uses reflection to see if you've put a method named "OnTriggerEnter2D" in your class and if you have then it will call that method as needed. Same with Start, Update, etc. Unity refers to this general reflection-to-know-about-your-methods thing as messages. Probably not the best descriptor, but it is what it is.
Hi, and thanks for the input. I don't think the answer got me totally there, but it's definitely helpful, thank you.
It sounds like you're saying that OnTriggerEnter2D is not pre-defined by Unity, which partially answers my question, since part of my confusion is that I thought OnTriggerEnter2D was already defined. But you're saying it's not defined, so that leads me to think that a $$anonymous$$essage is somewhat like a handle or nickname that we use in the script to interact with something but we still have to define what the interaction does?
I'm going to keep meditating on this, thanks! If anyone else can shed more light on my question based on the script I shared, please feel free to chime in.
public class LoseCollider : $$anonymous$$onoBehaviour {
private Level$$anonymous$$anager level$$anonymous$$anager;
void OnTriggerEnter2D(Collider2D trigger) {
level$$anonymous$$anager = GameObject.FindObjectOfType<Level$$anonymous$$anager>();
level$$anonymous$$anager.LoadLevel ("Lose Screen");
}
EDIT: and I am definitely looking at the Send$$anonymous$$essage literature, thanks!
Think of it this way. A message is a special method with a specific name and IF you define that method in your class then Unity will call that method at the appropriate times; you DON'T call the method yourself. For example IF you define a method named Update in your $$anonymous$$onoBehaviour, then Unity will call that method once per frame, you don't call Update yourself. If you define an OnTriggerEnter2D method, then Unity will call that method whenever a 2D trigger-collision happens to the game object with the script attached.
A message is a special method with a specific name and IF you define that method in your class then Unity will call that method at the appropriate times; you DON'T call the method yourself.
Yes, thank you! This is what I was looking for! I get it.
And actually, you also shed some light on the Update method which I didn't even realize I didn't fully understand. You're right, the Update method is not always used in my scripts, but that doesn't mean the game is not updating. It just means I am not telling this particular object to do anything on the frame update.
If I may state the obvious out loud for a moment... this is a re$$anonymous$$der that lots of code is running in the game that I don't see in any of my scripts. So the OnTriggerEnter2D function already exists and can get called independently of my script (because Unity is tracking all of the game's objects and their movements) but nothing will happen when an object triggers my object unless I tell Unity what I want to happen, by defining the method in the script that is attached to the object.
Answer by Owen-Reynolds · Jan 07, 2016 at 11:29 PM
They're just telling you that the Physics system will automatically call that function at the right time.
One way of saying that is: the system sends an OnTriggerEnter message, which is picked up by your OnTriggerEnter function.
I prefer thinking of OnTriggerEnter as a callback. You write it, plug it in, and the system knows to call it when it needs to. It's the same either way. But a typical callback lets you name the function whatever you like, and you write the line plugging it in. So it sort of feels a little more messagey.
Thanks for your comment Owen. In conjunction with the answer above, it confirms my new understanding.