- Home /
Using Struct to SendMessage - Number of parameters error.
Hi guys,
I have been reading up on how to send multiple variables with SendMessage. The simplest way that I 'thought' I could understand was using was using a struct.
I have this at just above the void Awake() function...
public struct ExplosionParameters
{
public float explosionForce;
public float explosionUpForce;
public Vector3 explosionPosition;
}
I then try to send a message to the other script...
hit.gameObject.SendMessage("ExplosionInfo", new ExplosionParameters{ explosionForce = Force, explosionUpForce = UpForce, explosionPosition = transform.position } );
This has 3 Parameters.
The other script has the following void...
public virtual void ExplosionInfo(float eF, float eUF, Vector3 eP)
{
explosionForce = eF;
explosionUpForce = eUF;
explosionPosition = eP;
}
This also has 3 parameters.
But, I am getting the following error...
Failed to call function ExplosionInfo of class vp_DamageHandler Calling function ExplosionInfo with 1 parameter but the function requires 3.
What am I doing wrong?
Many thanks for looking!
Sincerely,
Bruce
Answer by DannyB · Apr 27, 2013 at 06:44 AM
It would seem that you are trying to send the struct (one parameter) to ExplosionInfo, which expects three parameters.
Try creating a ExplosionInfo( ExplosionParameters info )
method.
Struct example:
struct MessageInfo {
public string msg;
public int number;
public MessageInfo( string msg, int number ) {
this.msg = msg;
this.number = number;
}
}
Send message like this:
receiver.SendMessage( "GetMessage", new MessageInfo( "Hi", 3 ) );
Receive the message like this:
void GetMessage( MessageInfo info ) {
Debug.Log(info.msg);
Debug.Log(info.number);
}
Hi Danny,
$$anonymous$$any thanks for your response! Have been trying to work out 'ExplosionInfo( ExplosionParameters params )' after I read your response, so am not ignoring you hehe.
So the struct is is seen as one parameter rather than the 3 inside it, that makes sense.
Will keep working away. At the moment it doesn't like the 'params' part but it is highlighted in blue so it defintely recognises it.
Cheers!
Oh, my bad. Just rename "params" to something else. Say "info".
I have updated the answer.
Your answer
Follow this Question
Related Questions
Are named parameters supported? 1 Answer
SendMessage with multiple parameters 3 Answers
How to store a Transform[,] grid in an ArrayList? 2 Answers
Parameter doesnt exist 1 Answer