Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

SendMessage says no receiver found.

Discussion in 'Scripting' started by kevin067, Nov 1, 2009.

  1. kevin067

    kevin067

    Joined:
    Oct 23, 2009
    Posts:
    22
    Sorry if this is dumb question. But I am scripting in c#

    and simply added this in one gameobject

    Code (csharp):
    1. void ApplyDamage (float damage) {
    2.     print (damage);
    3. }
    4.  
    and then called it from another totally different gameobject

    Code (csharp):
    1. SendMessage("ApplyDamage",5.0);
    I get no compile error, only runtime error.

    SendMessage ApplyDamage has no receiver!

    So what am I missing?
     
  2. andor_p

    andor_p

    Joined:
    Jun 20, 2009
    Posts:
    29
    If you want to send a message from GameObject A to a different GameObject B, you need to call the SendMessage method on B. So you need to obtain a reference for B, and then call B.SendMessage(...)
     
    Kiwasi likes this.
  3. kevin067

    kevin067

    Joined:
    Oct 23, 2009
    Posts:
    22
    That can't be right, because I truly do need to broadcast to many objects without knowing the reference to it.

    For instance I have a level1 script. I want to alert many objects that the the main character is dead. And also to tell level1 to start transitioning out.

    But next level will use the same main character, but the reference to level1 script will be gone and different objects, and only level2 script will be there.

    I truly want transmit a message without knowledge of the destination, no hard references.
     
  4. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    Hello,

    Can you use a manager GO as a receiver and broadcaster and don't destroy on load the GO?

    Ray
     
  5. Timmer

    Timmer

    Joined:
    Jul 28, 2008
    Posts:
    330
  6. CarpetShampooer

    CarpetShampooer

    Joined:
    May 7, 2015
    Posts:
    7
    Note: I get the same "no receiver found" error if I have an incorrect/misspelled function name as the first parameter passed via sendmessage. ex: I have a function "void DoThat()" and I try to sendmessage like Prefab.SendMessage("DoThis");
     
    ritesh_khokhani likes this.
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    6 year old thread.... and yes you need to spell the function correctly and that function needs to exist on the targetted gameobject for it to work.
     
  8. anhptoe

    anhptoe

    Joined:
    Dec 1, 2016
    Posts:
    5
    7 years old thread... and I still having this error. Function name correctly spelled.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Clear evidence that user error is just as prevalent today as seven years ago.

    SendMessage works. As long as you call it on a GameObject that has a component with the appropriate method.

    Of course most of the cool kids are using interfaces these days instead. SendMessage is considered obsolete by many.
     
    Joe-Censored and SparrowGS like this.
  10. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    Hi, sorry to dig this old thread but is there a way to test if the GameObject has a component with the appropriate method ?

    like doing : "hit.transform.gameObject.SendMessage("OnMouseDown", hit.collider)" but only if i can ?
     
  11. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    Do not use SendMessage. It's bad. Slow, resource-greedy and very bad design. Even with my standards. If you want, use events or actions instead (either Unity Events or C# Events).
     
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Implement an interface. Then call GetComponent on the interface.

    Or use ExecuteEvents, which does exactly the same thing.
     
  13. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Imo it was obsolete to begin with, the only excuse I can give it is explaining basics to absolute beginners and you can do so in better ways.

    Also, yay, I'm cool.
     
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Back in 2009 when this thread was created GetComponent did not work for interfaces. Which meant if you wanted to call a method which could have been on one of several components, SendMessage was your best option. The only other option was to get every component and then do reflection.

    These days, SendMessage is almost useless. (But not entirely, there are still a couple of edge cases where it makes sense).
     
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If you really do need to use SendMessage, you can pass in SendMessageOptions.DontRequireReciever. This means no error will be thrown if there is no receiving method.

    Its a really old school technique, which is why I didn't remember it straight off the bat. But it does work.
     
  16. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    Thanks for the replies, i'll dig into it