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
2
Question by shophongkong · Jan 26, 2015 at 09:46 AM · ui

New UI button OnClick() how to "send message" with parameters?

I have try different ways to send message with/without brackets, commas and different combinations to seperated text with parameters but couldn't get it to work. It'd be great if there is an example but I wasn't able to find it in the refereces. alt text

testsendmessage.png (5.3 kB)
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
6

Answer by nightowl79a · Jan 26, 2015 at 11:32 AM

alt text You might have to zoom in with ctrl + mouse scroll wheel to see the picture, but its just a string with nothing special about it. it calls this script:

 public void Testing123(string buttonName)
     {
         Debug.Log (buttonName);
     }

and that makes it work just fine.

Also the function must be public and void, and have one or no parameters. For the parameters you can use float, int, string, bool, or a Unity Object.

http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button This is a very nice video to help understand almost everything about the new UI buttons.


testing123.png (66.7 kB)
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 Baste · Jan 26, 2015 at 11:35 AM 0
Share

The image in your answer is giving me a 403. You could just upload the image as a part of your post ins$$anonymous$$d of using dropbox

avatar image nightowl79a · Jan 26, 2015 at 11:42 AM 0
Share

Yea sry about that, sometimes my pictures are too big to upload even though I just use the windows snipping tool.

avatar image shophongkong · Jan 27, 2015 at 09:41 AM 0
Share

Thanks for the info and please let me explain this a bit more, I'm trying to use the build-in 'Send$$anonymous$$essage' function. There is method name and parameter. The Send$$anonymous$$essage is used differently, I don't want to call a method in the same gameobject/class. So I want to use Send$$anonymous$$essage ins$$anonymous$$d of the solution you showed.

avatar image nightowl79a · Jan 27, 2015 at 11:08 AM 0
Share

What you could do is set the gameobject that needs to receive the message in the editor, or use a gameobject.find, then in the function activated by the button do something like this:

 public GameObject whatGameObject;
 // Or use GameObject.find()
 
     public void SendA$$anonymous$$essage(string what$$anonymous$$sg)
     {
         whatGameObject.Send$$anonymous$$essage ("thefunction", what$$anonymous$$sg); 
     }

The button would activate the SendA$$anonymous$$essage function, and send the 'what$$anonymous$$sg' variable. Then you would send the message in the function. Ofc if you needed multiple parameters you might need to find a different solution.

avatar image
3

Answer by NullTerminated · Aug 15, 2016 at 03:08 PM

 void InitMusicListContent()
         {
             JSONNode musicData = _myAudioPlayer.GetMusicData;
 
             //set the size of it before we start to fill it
             _musicListRectTransform.sizeDelta = new Vector2 (_musicListRectTransform.rect.x, _heightOfSongPanel * (musicData.Count + 2));
 
             // for each song in the array, create new panel--> Parent under the Content panel --> Get its components --> init them
             for (int i = 0; i < musicData.Count; i++) 
             {
                 GameObject songPanel = 
                     Instantiate (_songPanelPrefab, this.transform.position, this.transform.rotation, this.transform) as GameObject;
 
                 Text[] textComponents = songPanel.GetComponentsInChildren<Text> ();
                 foreach (Text text in textComponents) 
                 {
                     if (text.name == "ArtistNameText") {
                         text.text = musicData [i] ["ArtistName"];
                     } else if(text.name == "TrackNameText"){
                         text.text = musicData [i] ["TrackName"];
                     }
                 }
 
                 Button songPanelButton = songPanel.GetComponentInChildren<Button> ();
 
                 // must assign it to a variable before supplying it to listener
                 string x = musicData[i]["TrackPath"];
                 songPanelButton.onClick.AddListener(() => {_myAudioPlayer.AddToMusicQueue(x);} );
             }
         }


So i was working on some music streaming in a game engine and i ran into the same problem. I wanted the user to be able to choose a song to add to there music Queue while they were playing the game. Because of the sheer amount of songs to choose from, the only good way to subscribe a button to a specific method was programatically. Here, you can see that I have a loop that instantiates a prefab, and on this prefab I had a Button. I needed to subscribe this button to a method that took in a string that represented a path to the song. Because each of these are different for each song, I needed the parameter value that is passed in to be diff for each button. Initially, It didnt work because i did this:

 songPanelButton.onClick.AddListener(() => {_myAudioPlayer.AddToMusicQueue(musicData[i]["TrackPath"]);} );

I tried to pass in the track path directly. Once I placed the value in a variable, then it worked. Don't make this same mistake. Its hard to fix. Hope this helps. Cheers mate.

Comment
Add comment · 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
1

Answer by VRAndAR · Jan 22, 2018 at 07:37 AM

Hopefully This will Help You.If you want to pass the parameters through the inspector, Then make them as the public variables in the script in which the function which calls when the button is present.

 public class ButtonScript : MonoBehaviour {
 
     public string message;
     public int integer;
     public float floatingPointNumber;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     public void ButtonPressed()
     {
         //Do Whatever You Want
         Debug.Log("Message: " + message + "\t" + "Integer: " + integer.ToString() + "\t" + "Float: " + floatingPointNumber);
     }
 
 }
 
Comment
Add comment · Show 1 · 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 CrandellWS · Jul 22, 2021 at 04:25 PM 0
Share

I was about to give this as an answer then i found you have already suggested using a class it self to hold and pass the data... this is an easy way to work around for only having 1 parameter or none but passing in many variables at the same time.... I hope more people see this answer

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 4.6 new UI System. Button not working after the canvas is created as prefab. HELP!! 1 Answer

UI.Toggle set to isOn initial state on exit 1 Answer

Can I break the UI MASK hierarchy? 2 Answers

How to stop non-rectangular buttons from overlapping? 4 Answers

How can you set navigation type in code? 0 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