Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Feb 26, 2018 at 02:19 PM by Vencarii for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Vencarii · Feb 25, 2018 at 08:55 PM · uibuttonobject

How can I bind a custom object or variable to an UI button?

Hello,

I have a ScriptableObject that holds some data for a song object. I created multiple instances of that ScriptableObject and saved them as assets, one for each song.

I instantiate multiple UI Buttons at runtime, the button text is the title of a song. That works fine, but how do I know which song I chose when I click a button? How can I bind a song object to an UI button?

Do I have to write a custom button class and inherit from Button somehow? Tips are very much appreciated. :-)

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Feb 25, 2018 at 10:36 PM

I will assume the button creation and you will have to turn that into what you actually have (since you did not expose your code).

We going to consider that each of your ScriptableObject contains a songName variable.

 public MyScriptableObject [] scriptables;
 void CreateButtons()
 {
       foreach(MyScriptableObject so in scriptables)
      {
              GameObject obj = new GameObject(so.songName);
              Button button = obj.AddComponent<Button>();
              // Now the tricky part
              MyScriptableObject temp = so;
             button.onClick.AddListener(()=>{ Debug.Log(temp.songName); });
      }
 }

That code should print the name of the song contained in the ScriptableObject on press. There is the tricky part that needs a quick explanation. In a foreach loop the compiler does some magic on the background and long story short, if you don't do the trick in the temp variable, all your buttons will point to the last ScriptableObject of the array. So you need to store in a temporary to fix the problem. Not a Unity issue, it is a C# limitation.

The AddListener is the method to add a delegate to the button on click.

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
0

Answer by Vencarii · Feb 26, 2018 at 06:25 AM

I had a similar idea about an hour after I posted my question, but I still didn't get it to work. But your code helped me to fix the problem. I use this methid now:

 public void ShowSongList()
     {
         GameObject canvas = (GameObject)Instantiate (songMenuPrefab) as GameObject;
         GameObject menu = canvas.transform.Find("Panel").gameObject;
 
         List<Song> songs = _gameManager.GetAllSongs ();
 
         for (int i=0; i<songs.Count; i++) {
 
             // Create song item
             GameObject item = (GameObject) Instantiate (songItemPrefab) as GameObject;
 
             // set menu button label
             item.transform.Find ("SongTitle").GetComponent<Text> ().text = songs [i].songName;
 
             // onClick Event
             // Storing the song in a tmp variable helped me! I tried to use OnClickSongItem(songs[i]), that didn't work
             Song tmpSong = songs[i];
             item.GetComponent<Button> ().onClick.AddListener (() => OnClickSongItem(tmpSong));
 
             // add item to panel
             item.transform.SetParent (menu.transform, false);
         }
 
         canvas.SetActive (true);
     }
 
     public void OnClickSongItem(Song song)
     {
         Debug.Log (song.songName);
     }
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 fafase · Feb 26, 2018 at 01:27 PM 0
Share
   GameObject item = (GameObject) Instantiate (songItemPrefab) as GameObject;

you don't need casting before and after. Just pick one, I'd remove the as GameObject.

For the rest you seem to get it right. I'd use some global references ins$$anonymous$$d of looking for SongTitle each iteration.

Follow this Question

Answers Answers and Comments

136 People are following this question.

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

Related Questions

UI button does not press if colliding with other UI object 1 Answer

How to rotate boject using UI button? 3 Answers

How to display UI elements in front of the gameObject? 1 Answer

I need to move a sprite when an UI button is clicked 1 Answer

Can't interact with world space ui button when cursor mode is locked any solutions? 1 Answer


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