Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by MattiGroot · Sep 12, 2014 at 06:39 PM · sendmessage

Unity SendMessage with Parameter not working

Dear Unity Answers Community,

Though the title is somewhat unclear, I think I can explain it easily below: My mouse input simply checks if it hit an object, if it does. It sends a message to the object and stores the object in a variable. When the mouse input hits another object, I wanted to send a message with the already defined object as parameter. So the receiving script can target that object. Here are the two functions that matter:

 void MouseClick(){
         beginMousePosition = Input.mousePosition;
         panVector = GetMousePos ();
         Ray ray = Camera.main.ScreenPointToRay(beginMousePosition);
         RaycastHit hit ;
         if (Physics.Raycast (ray, out hit)) {
             if(hit.collider.transform.tag == "SelectionCollider"){
                     GameObject objectToSend = selectedObject;
                     hit.transform.SendMessage("OnSelected", objectToSend);
                     selectedObject = hit.transform.gameObject;
             }
             else {
                 if(selectedObject != null){
                     hit.transform.SendMessage("OnSelected");
                     selectedObject = null;
                 }
             }
         }
     }


Receiving function:

 void OnSelected(){
         if(!selected){
             Select ();
         } else {
             UnSelect();
         }
     }
     
     void OnSelected(GameObject selectedObject){
         Debug.Log ("Not receiving in this function");
         if (!selected) {
             Select();
         } else {
             if(selectedObject == null){
                 UnSelect();
             }
             else {
                 HandleSecondSelect(selectedObject);
             }
         }
     }

The first time I select an object with my mouse, it enters the empty receiving function as it needs to. However the second time (when the objectToSend has a reference) it's also received in the empty receiving function.

Does anybody know why the 1 parameter receiving function doesn't get triggered?

Thanks in advance,

Greatings Matti.

Comment
Add comment · Show 6
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 rutter · Sep 12, 2014 at 06:40 PM 0
Share

I'm not sure if Send$$anonymous$$essage plays nice with overloaded functions. You might need two distinct names to call, or you could pass null, or find some other workaround.

avatar image MattiGroot · Sep 12, 2014 at 07:08 PM 0
Share

I've added the overloaded functions later, because when I didn't have the empty parameter function, unity gave an error saying that the receiver asks for 1 parameter. Which I most certainly passed with Send$$anonymous$$essage. I know there are some workarounds, but this approach would really be nice in my case. Edit: There was a case when I passed down null, im going to check if that was the problem. 2nd Edit: Didn't seem to be the problem.

avatar image yoyo · Sep 13, 2014 at 06:42 AM 0
Share

Just pass null when you don't want to pass an object (line 14 in your first code snippet). You already have a null check in the receiver, so that Should Just Work.

avatar image vexe · Sep 13, 2014 at 07:02 AM 0
Share

Whenever I see a usage of Send$$anonymous$$essage, I ask, "Why not delegates?"

avatar image giulio-pierucci · Sep 13, 2014 at 07:11 AM 0
Share

Call 2 functions with two distinct names, how rutter has already write.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

SendMessage has no receiver (SmartFox) 1 Answer

SendMessage to another specific GameObject? 1 Answer

iPad touch scripting... 0 Answers

How to send sms from unity in ios platform 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