- Home /
SendMessage with multiple parameters
I've been trying to use SendMessage with multiple parameters and I found this answer to solve my problem. It's written in UnityScript and I tried converting it to C#:
public class Parameters {
private int damage;
private Transform sender;
void Message (int damage, Transform sender) {
this.damage = damage;
this.sender = sender;
}
}
public class PlayerAttack : MonoBehaviour {
IEnumerator MeleeAttack (int damage, string attackType) {
animation.Play(attackType);
yield return new WaitForSeconds(animation[attackType].length);
target.SendMessage("ApplyDamage", new Parameters(damage, transform));
}
}
But I get this error that 'Parameters' has no constructor that takes 2 arguments, but it does. Is this a conversion problem? If not, what did I do wrong? Thanks in advance.
Answer by Seth-Bergman · Jul 24, 2012 at 10:09 PM
Shouldn't you be calling Message, rather than Parameters?
ps, not sure if it's just me, but I can't access the link provided..
You would think so. I already tried calling $$anonymous$$essage, and on inspection of the answer I found, they call the class, not the constructor itself.
I also fixed the link.
Answer by Akill · Aug 04, 2012 at 11:17 AM
You don't have a constructor in your Parameters class. You are trying to construct an instance of Parameters using a function. A constructor must have the exact same name as the Class.
Answer by DanielDollerup · Apr 03, 2014 at 08:46 AM
You could also just pass an object
Example
object[] parameterObject = new object[NumberOfParameters]{object1, object2, object3 ...}; SendMessage("MessageToSend", parameterObject);
Just remember to parse the objects in the array to the respective objects on the object you are sending a message to.
Hope it helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220613074643im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
SendMessageUpwards - Send Multiple Parameters? 3 Answers
Handling multiple tags 1 Answer
Struct for holding Class Parameters 0 Answers
SendMessage to multiple objects 2 Answers
UI Button multiple parameters 6 Answers