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
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);
}
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.
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.
Like @BerggreenDK said, you could use a Struct, a Class, an Object to have multiple params.
Here is an exemple with a Struct :
public class Myclass : MonoBehaviour
{
public struct Arguments
{
public float someFloat;
public Vector3 someVector3;
public Arguments(float someFloat, Vector3 someVector3)
{
this.someFloat = someFloat;
this.someVector3 = someVector3;
}
}
public void DoSomething (Arguments args)
{
// Do whatever with args.someFloat and args.someVector3
}
}
Usage :
gameObject.sendMessage ("DoSomething", new Myclass.Arguments (3f, transform.position));