Ok, I know this question has been asked a million times, but I can’t get mine to work (I’m very new to Unity and JS).
I’m using this to send the message, with my_object assigned to the GameObject.
my_object.SendMessage("LifeNumber", lives_lost);
and this, on another script, to receive it
function LifeNumber(lives_lost){
//code
}
This should work, right?
It doesn’t, instead I get an error saying
“SendMessage LifeNumber has no receiver!”
Help!
Some points:
- Maks sure that your script is actually attached to the my_object gameobject.
- If the script is attached to a child object of my_object it won’t work. In this case you would have to use BroadcastMessage. Keep in mind that has more overhead than SendMessage.
- If the script is attached to a parent object of my_object, it won’t work either. For this case there’s SendMessageUpwards. Like BroadcastMessage it has a greater overhead.
- If you just want to send the message to an arbitrary object where you don’t know if it has a script or not, you can use an additional parameter for SendMessage / BroadcastMessage / SendMessageUpwards and specify SendMessageOptions.DontRequireReceiver. In this case the function tries to send the message but don’t complain when there is no receiver.
- You should specify the functions parameter type, so Unity can actually find the function. A function signature is defined by the function name and the count and type of the parameters.
So the function declaration should look like this:
function LifeNumber(lives_lost : int){
// [...]
}