- Home /
How do i Specify a Receiver for a Send Message Function?
Help! i am not looking to use: SendMessageOptions.DontRequireReceiver.
I have looked up on this in the script reference and the forums. I'm wondering how i would be able specifically give an game-object/script-function a reciever so the messages dosent relay anywhere else.
So far when i use: SendMessageOptions.RequireReceiver i get a dubug log saying e.g no receiver found, how can i get the sendmessage to get picked up by a component? whats the correct way to use SendMessageOptions.RequireReceiver. Thank you.
Antione.
If you only want to call a method in one script what's stopping you from using GetComponent?
Answer by Molix · Apr 29, 2010 at 03:47 PM
SendMessage is a way of broadcasting a message to "anyone that is listening". The option RequireReceiver just lets you know (via an error) if "no one was listening".
If you want to send the message to a specific recipient, you can just get a reference to it (e.g. via GetComponent) and call it. e.g. (C#):
Instead of:
SendMessage("MyFunction");
You'd do:
MyScript myScript = GetComponent(typeof(MyScript)) as MyScript;
myScript.MyFunction();
The JS version is very similar:
var myScript : MyScript= GetComponent(MyScript);
myScript.MyFunction();
Ah yes i see, thanks. Nice one thanks for the quick reply too, im going to test it now, but i will have to translate it to javascript.
could you post it on here the java translated one please it would help alot
We should explain, that "anyone that is listening" means any Component, that is attached to the GameObject. So for example, if you want every Unit to attack a base, you can't simply call Send$$anonymous$$essage("attackEnemyBase").
Answer by Sinncere · Apr 29, 2010 at 04:20 PM
Thanks agen just saw this in the script reference, in any case others are intrested.
Your answer
Follow this Question
Related Questions
Send Message Has no Receiver 1 Answer
SendMessage , how can i send two parameter 3 Answers
Message not being recieved 1 Answer
Collision script 1 Answer
GameObject not Scaling Correctly after Receiving Message 1 Answer