Multiple arguments using SendMessage

I’ve been trying to get multiple arguments to work with SendMessage but keep getting an error. BCE0024: The type ‘DamageInfo’ does not have a visible constructor that matches the argument list ‘(int, String)’.

Not sure what I’m doing wrong.

My Class

class DamageInfo {

	var damage : int = 0;
	var spellType : String = "";


	function damageInfo ( damage : int, spellType : String ) {

		damage = damage; 
		spellType = spellType;

	}

}

My SendMessage. Where the error is occurring.

SendMessage("damagePlayer", new DamageInfo(Random.Range(15,20), "melee"));

i havent used this so i dont know for sure, but shouldn’t it be:

SendMessage("damageInfo",Random.Range(15,20), "melee");

Normal SendMessage can only take one argument. That’s why the class is needed or it won’t work from what I’ve tried.

Create an object:

#pragma strict

public class TestScript2 extends MonoBehaviour
{
    function Start()
    {
        var ts:TestClass2 = new TestClass2();
        ts.i = 15;
        ts.s = "my test string";
        SendMessage("TestMethod", ts);
    }

    function TestMethod(ts:TestClass2)
    {
        Debug.Log(ts.i + " " + ts.s);
    }
}

public class TestClass2
{
    public var i:int;
    public var s:String;
}

hmmm… good point. My line up there got rid of the error message you mentioned in the first post but then caused another error due to what you just said. Maybe using GetComponent would be easier and also allow you to send multiple arguments? Kind of like shown here:
http://forum.unity3d.com/threads/180328-SendMessage?p=1234891&viewfull=1#post1234891

edit - thats interesting appels… thats looks easier.

You done something wrong, made a test case that works:

#pragma strict

class myClass {
	var anInt : int = 0;
	var aString : String = "";
	function myClass (i:int, s:String){
		anInt = i;
		aString = s;
	}
}


function Start () {
	SendMessage("someReciever", new myClass(Random.Range(0,10),"lol"));
}

function someReciever (incoming : myClass) {
	print(" recieved " + incoming.aString);
}

Though if it was me, id get the recievers component and just call the function directly and pass whatever i want.

It looks like my constructor wasn’t named the same as my class and the variables needed to be a different name. Got it all working now. Thanks for the help!

That should be “this.damage = damage”; otherwise you’re just assigning the variable to itself.

–Eric