• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by bisaniyehocam · Dec 18, 2010 at 02:09 AM · sendmessagemessagesend

SendMessage , how can i send two parameter

i made a function needs two parameter (damage : float , mod : float) when i made from another script : avatar.SendMessage(16,1) gives error. it tell me send message sends one parameter , function needs two. how can i send two parameters , srry for my eng. thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by BerggreenDK · Dec 18, 2010 at 02:26 AM

You cant. Thats been my experience too.

As the "SendMessage" reference says here:

http://unity3d.com/support/documentation/ScriptReference/Component.SendMessage.html

function SendMessage 
(methodName : string, value : object = null, options : SendMessageOptions = SendMessageOptions.RequireReceiver) : void

methodName is the "function" you want to call on everyobject.

object is the value (ONE object)

BUT, you are not restricted to send a simple object (like integer). You can create your own "class/structure/object" and pass that one along as an object.

So make your own "package" of values you need to broadcast. Init that object and then send it.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BerggreenDK · Dec 18, 2010 at 02:50 AM 0
Share

Alternatively you could use GetComponent to grab the scripts of the objects you want to "hit".

http://answers.unity3d.com/questions/8790/whats-the-difference-between-sendmessage-and-getcomponent

avatar image petersvp · Sep 28, 2016 at 11:23 PM 0
Share

You can safely pass Array of Objects as an object, and cast them back, like

 public void UtilsValueToFloat(object args)
 {
     object[] a = (object[])args;
     object obj = a[0];
     string prop = (string)a[1];
     float val = (float)a[2];
avatar image
10

Answer by -hiTo- · May 01, 2012 at 04:13 AM

Actually, the SendMessage function takes 1 argument, but that argument could be an array. So just make a temporary object[] array, fill it with all your values, and fire it away.

 string a = "abc";
 string b = "def";
 string c = "ghi";
 string d = "jkl";
 
 private void Start()
 {
     object[] tempStorage = new object[4];
     tempStorage[0] = a;
     tempStorage[1] = b;
     tempStorage[2] = c;
     tempStorage[3] = d;
 
     SendMessage("MessageRecieved", tempStorage);
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rosa-Gao · Feb 07, 2013 at 07:29 AM 0
Share

it works! thanks! But, It's possible to make dynamic 'new object[]'? just like 'push()' in JavaScript.

avatar image starikcetin · May 19, 2015 at 08:30 PM 0
Share

Rosa Gao You can use Generic Lists.

avatar image
0

Answer by unitydev0008 · Jan 21, 2011 at 09:40 AM

Sorry to answer an already answered question and it being so delayed, but you can send multiple parameters with a sendmessage in a sense. The way i have been doing it is make the function you need take a Vector2 argument in your case since you have 2 floats and then simply call Sendmessage and pass it a new vector2 with the floats u want to pass.

Example in C#:

Reciever:

private void AdjustHealth(Vector2 damageMod) { // Break the vector 2 into two floats float damage = damageMod.x; float mod = damageMod.y;

// Do your code }

Sender:

// Or however you trigger your sendmessage void OnTriggerEnter(Collider other) { other.SendMessage("AdjustHealth", new Vector2(16,1));

}

Again sorry for late answer just trying to help out =P

Note: u can also do this with vector3 to send 3 parameters.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image burnumd · Jan 21, 2011 at 05:46 PM 0
Share

It's not a bad solution per se, but it's very brittle. What happens when you want to send 8 floats or two strings? There's no Vector8 to help you there. It's much preferred to bundle up your parameters in a custom Object as in the accepted answer.

avatar image unitydev0008 · Jan 22, 2011 at 10:12 PM 0
Share

yea i was just suggesting it as a quick solution since only 2 floats were needed. but u are correct if u have anything more than 3 numbers it is not going to work... just trying to provide a simple alternative =P

avatar image bisaniyehocam · Jan 23, 2011 at 06:25 AM 0
Share

i like that good idea

avatar image unitydev0008 · Jan 23, 2011 at 09:03 PM 0
Share

Glad to help Eren, again as burnumd stated this is not the best way to go about it but it is simple and if u are passing floats it works well. I use it all the time and u can even use ints by casting them as an int when u convert them in the adjusthealth function.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do i Specify a Receiver for a Send Message Function? 2 Answers

Collision script 1 Answer

SendMessage static parameters checking 0 Answers

Trouble sending message... 2 Answers

Send Message Has no Receiver 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges