Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
3
Question by gilbento · Jan 07, 2016 at 11:13 AM · c#scriptingbasics

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?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Jan 07, 2016 at 12:10 PM 1
Share

Please add a link, I wouldn't want to comment on what someone else's writing means without seeing what they've actually written.

avatar image gilbento Bonfire-Boy · Jan 07, 2016 at 10:24 PM 0
Share

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)."

avatar image tanoshimi · Jan 07, 2016 at 04:00 PM 0
Share

I don't see the word "message" anywhere? http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter2D.html

avatar image Bonfire-Boy tanoshimi · Jan 07, 2016 at 04:04 PM 0
Share

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.

avatar image gilbento tanoshimi · Jan 07, 2016 at 10:52 PM 0
Share

See my reply to Bonfire Boy above. Thanks!

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gilbento · Jan 07, 2016 at 10:58 PM 0
Share

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!

avatar image LessThanEpic · Jan 07, 2016 at 11:09 PM 1
Share

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.

avatar image gilbento LessThanEpic · Jan 07, 2016 at 11:39 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gilbento · Jan 07, 2016 at 11:43 PM 0
Share

Thanks for your comment Owen. In conjunction with the answer above, it confirms my new understanding.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make a loop in a C# script? 1 Answer

Performance peaks in ScriptRunBehaviourUpdate 1 Answer

Performance of event vs static method call 0 Answers

Why after importing Standard Assets my Visual Studio Solution has 3 projects ? 1 Answer

Inactive script still catching collision/trigger 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges