SendMessage Problem

Hi, I have a reoccurring problem when I try to communicate between scripts.

I usually use:

MyScript.SendMessage ("MyFunction", Variable)

but when I want to send two variables like this:

MyScript.SendMessage ("MyFunction", Variable1, Variable2)

it bugs up saying:
No appropriate version of ‘UnityEngine.Component.SendMessage’ for the argument list ‘(String, Variable Type, Variable Type)’ was found.

I figure it means that SendMessage does not support more than one Variable.
So I found out that when I wanted to send more than one variable I had to use this syntax:

MyScript.MyFunction (Variable1, Variable2)

but sometimes, not always, it bugs up saying:
‘MyFunction’ is not a member of ‘UnityEngine.Component’.

And I have never been able to send just one variable like this:

MyScript.MyFunction (Variable)

which gives exactly the same error.

Other than the fact that apparently I have to use SendMessage for 1 or less variables, and MyScript.MyFunction for more than 1 Variable, I haven’t been able to find any consistency in the occurrence of this problem, sometimes it works flawlessly, other times it bugs up like this.

I have tried searching the forums but I have not been able to find any post that had a working solution for this problem, and the only thing I can think of is the possibility that some function names just doesn’t work?

myscript.xxx does not make sense.
you need to fetch an object of that class first and then call that instance.MyFunction

MyScript.MyFunction only works with static functions and they have no knowledge over specific objects at all

as for the sendmessage error with 2 parameters: thats right, you need to create an array of object. In C# you do that through new Object {obj1, obj2, obj3, …} where X is the number of objects in the array you want to pass in.

of course I have declared MyScript as a Component Variable containing the script i need to communicate with, so it could for instance be:

var OnSwitch : boolean = true;
var TextString : String = ("The clock strikes twelve");
var MainScript : Component;

function somefunction(){
MainScript.StupidFunction(OnSwitch,TextString);
}

and the function would then look like this:

function StupidFunction(OnSwitch,TextString){
if (OnSwitch){
   print(TextString);
}
}

I am surprised if that should not work, cause it has worked for me on several occasions, just not all of them :wink:

I should add that I am not REALLY a programmer, but an animator, I am just the only person on the team with any programming experience, so even though I CAN script I might be a little thick!

We use JavaScript but a similar method can be used I recon like:

var AwesomeArray = New Array ()

?
And does the receiver then also have to be an Array? or is it possible for two variables to receive Array[0] and Array[1]?

Hmm I have tried making an Array and using that in a SendMessage like this:

var goTo : Vector3;
var arr = new Array();

arr.Push (goTo);
arr.Push (true);
playerActor.SendMessage ("GoTo", arr);

and receive like this:

function GoTo(arr){
var dest : Vector3;
var input : boolean;
	
dest = arr[0];
input = arr[1];

I get the following Error:
Type ‘Object’ does not support slicing.

My guess is that the array cannot contain both a ‘Vector3’ and a ‘boolean’…

So that pretty much gets me back to square one, I need to send two variables of different types to a function in another script.