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(…)
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.
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”);
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).
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.