Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
6
Question by erenaydin · 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
12
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 1
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
14

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 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 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 m-y · May 04, 2020 at 10:33 AM 0
Share

ok i sent from c Sharp the Array of string tempStorage[0] = a; tempStorage[1] = b;

how to use them separately in Json function ?

avatar image -hiTo- m-y · Feb 08, 2021 at 03:27 PM 0
Share

Can't you convert into json first and just send the whole string? Seems like a strange thing to do though - converting data into json to send them between objects in a scene instead of simply sending the raw data.

In any case, your question is too broad for a comment, and there's not enough information to really answer it. Create a new question with more information.

avatar image
1

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 erenaydin · 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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

BroadcastMessage Functionality Help 1 Answer

Make message to other gameObject stop. 1 Answer

How to Send Message to other GameObject 3 Answers

Player Character Health 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges