- Home /
SendMessage , how can i send two parameter
i made a function needs two parameter (damage : float , mod : float) when i made from another script : avatar.SendMessage(16,1) gives error. it tell me send message sends one parameter , function needs two. how can i send two parameters , srry for my eng. thanks
Answer by BerggreenDK · Dec 18, 2010 at 02:26 AM
You cant. Thats been my experience too.
As the "SendMessage" reference says here:
http://unity3d.com/support/documentation/ScriptReference/Component.SendMessage.html
function SendMessage
(methodName : string, value : object = null, options : SendMessageOptions = SendMessageOptions.RequireReceiver) : void
methodName is the "function" you want to call on everyobject.
object is the value (ONE object)
BUT, you are not restricted to send a simple object (like integer). You can create your own "class/structure/object" and pass that one along as an object.
So make your own "package" of values you need to broadcast. Init that object and then send it.
Alternatively you could use GetComponent to grab the scripts of the objects you want to "hit".
http://answers.unity3d.com/questions/8790/whats-the-difference-between-sendmessage-and-getcomponent
You can safely pass Array of Objects as an object, and cast them back, like
public void UtilsValueToFloat(object args)
{
object[] a = (object[])args;
object obj = a[0];
string prop = (string)a[1];
float val = (float)a[2];
Answer by -hiTo- · May 01, 2012 at 04:13 AM
Actually, the SendMessage function takes 1 argument, but that argument could be an array. So just make a temporary object[] array, fill it with all your values, and fire it away.
string a = "abc";
string b = "def";
string c = "ghi";
string d = "jkl";
private void Start()
{
object[] tempStorage = new object[4];
tempStorage[0] = a;
tempStorage[1] = b;
tempStorage[2] = c;
tempStorage[3] = d;
SendMessage("MessageRecieved", tempStorage);
}
it works! thanks! But, It's possible to make dynamic 'new object[]'? just like 'push()' in JavaScript.
ok i sent from c Sharp the Array of string tempStorage[0] = a; tempStorage[1] = b;
how to use them separately in Json function ?
Can't you convert into json first and just send the whole string? Seems like a strange thing to do though - converting data into json to send them between objects in a scene instead of simply sending the raw data.
In any case, your question is too broad for a comment, and there's not enough information to really answer it. Create a new question with more information.
Answer by unitydev0008 · Jan 21, 2011 at 09:40 AM
Sorry to answer an already answered question and it being so delayed, but you can send multiple parameters with a sendmessage in a sense. The way i have been doing it is make the function you need take a Vector2 argument in your case since you have 2 floats and then simply call Sendmessage and pass it a new vector2 with the floats u want to pass.
Example in C#:
Reciever:
private void AdjustHealth(Vector2 damageMod) { // Break the vector 2 into two floats float damage = damageMod.x; float mod = damageMod.y;
// Do your code }
Sender:
// Or however you trigger your sendmessage void OnTriggerEnter(Collider other) { other.SendMessage("AdjustHealth", new Vector2(16,1));
}
Again sorry for late answer just trying to help out =P
Note: u can also do this with vector3 to send 3 parameters.
It's not a bad solution per se, but it's very brittle. What happens when you want to send 8 floats or two strings? There's no Vector8 to help you there. It's much preferred to bundle up your parameters in a custom Object as in the accepted answer.
yea i was just suggesting it as a quick solution since only 2 floats were needed. but u are correct if u have anything more than 3 numbers it is not going to work... just trying to provide a simple alternative =P
Glad to help Eren, again as burnumd stated this is not the best way to go about it but it is simple and if u are passing floats it works well. I use it all the time and u can even use ints by casting them as an int when u convert them in the adjusthealth function.
Your answer
Follow this Question
Related Questions
How do i Specify a Receiver for a Send Message Function? 2 Answers
BroadcastMessage Functionality Help 1 Answer
Make message to other gameObject stop. 1 Answer
How to Send Message to other GameObject 3 Answers
Player Character Health 2 Answers