- Home /
SendMessage StartSending has no receiver!
I am trying to call a method of a script attached to a different object. I have the following code, and I've attached the object to the localPlayerObject variable in the inspector, but I am still getting an error saying there is no receiver.
public Transform localPlayerObject;
private void SpawnLocalPlayer() { localPlayerObject.SendMessage("StartSending"); }
I tried Debug.Log(localPlayerObject.name); and it gave me the correct name, so I know I am referencing it correctly. And yes, StartSending is declared in the other script:
void StartSending() {
sendMode = true;
}
As far as I can see, what you have - as written - should work. This means the problem must be somewhere else, presumably in how you have your objects and scripts set up in the hierarchy.
$$anonymous$$ake sure:
- You aren't mixing script types (c# and JS)
- You have the script containing the "StartSending" method attached to the "localPlayerObject" (not one of its children)
Hm, I've just seen Eric5h5's answer and he's probably right! :-)
Actually, mixing script types is a perfect reason to use Send$$anonymous$$essage, as opposed to other ways like GetComponent. Send$$anonymous$$essage is totally runtime so it doesn't depend on compile order at all.
This probably is no necessary any more but here is what I found, having the same problem.
Theory: To send a message you receiver should be in the game object as the one you are sending it. It would look useless but the whole idea is to add scripts to an object without bothering about references, so different script doing different things having a function with the same name can be attached and remove to the same object on different times during runtime. Component.Send$$anonymous$$essage
The problem: Since you are just sending a message but your receiver is in another game object, its impossible for the message to be receive across game objects.
Solutions:
having a reference to the object with function you want to send the message to and calling its function directly (as Send$$anonymous$$essage would be pointless)
having a father/root in common, taking its reference and using Broadcast$$anonymous$$essage function to call the function, kind of like:
transform.root.Broadcast$$anonymous$$essage("myFunction")
of course this could lead to calling all the functions with that name
sort your game objects by purpose nesting into empty GameObjects, having a manager to reference the head of each group and call Broadcast$$anonymous$$essage on each head
and... well, thats all that came to my $$anonymous$$d, I used the first solution since it blend well with my problem.
hope this help anyone,
cheers,
First this question is 2 years old and has already been answered...
Second, Your theory is completely wrong :D Since you call the Send$$anonymous$$essage function of another object, the Send$$anonymous$$essage function invokes the function on it's attached $$anonymous$$onoBehaviours. Since the function belongs to the target object (or a component on that object) it doesn't matter who calls the function.
Answer by Eric5h5 · Apr 13, 2010 at 09:11 PM
The only way I can see that wouldn't work is if the object you dragged onto the localPlayerObject
variable was a prefab instead of an instance of a prefab.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162419im_/https://answers.unity.com/themes/thub/images/avi.jpg)