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

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);
}