SendMessage says no receiver found. (400661)

Sorry if this is dumb question. But I am scripting in c#

and simply added this in one gameobject

void ApplyDamage (float damage) {
    print (damage);
}

and then called it from another totally different gameobject

SendMessage("ApplyDamage",5.0);

I get no compile error, only runtime error.

SendMessage ApplyDamage has no receiver!

So what am I missing?

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(…)

1 Like

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.

Hello,

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

Ray

But I doubt you’ll want to use that often. If you’re building around this type of system for many game events look into an event manager system as noted above.

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”);

1 Like

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.

7 years old thread… and I still having this error. Function name correctly spelled.

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.

2 Likes

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 ?

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

Implement an interface. Then call GetComponent on the interface.

Or use ExecuteEvents, which does exactly the same thing.

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.

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

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.

Thanks for the replies, i’ll dig into it