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 /
  • Help Room /
avatar image
0
Question by joelgarcia · Sep 02, 2016 at 07:07 PM · buttonlistindexof

How do I reference button clicked to use in IndexOf

I'm trying to use a reference to a Button to get its position in a List of Buttons.

I have an OnClick function attached to multiple buttons. I have made a List of these buttons so that I can do other things with them by referencing them by their index numbers. I am having trouble figuring out how to get the index of the button that was clicked. I want to use IndexOf but I can't figure out what to use to reference the button inside of IndexOf. I have read through many pages of documentation and searched results but haven't been able to wrap my head around it.

Do I need to use AddListener? Do I need to use UnityEngine.EventSystems? Any input is appreciated. Here's an example of my script:

 public void SelectThisButton() {
 
     //try and get the index of the button pressed
     buttonIndex = buttonList.IndexOf( what goes in here? );
 
     //use the index of the button pressed for stuff
     buttonSprite = buttonList[buttonIndex].GetComponent<Image>().sprite;
 
     //do other stuff
 
 }
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

1 Reply

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

Answer by TBruce · Sep 02, 2016 at 07:13 PM

Why over complicate it? If all you what to do is retrieve the sprite image from the button clicked, this will work

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ButtonHandler : MonoBehaviour
 {
     public void ButtonPressed (Button button)
     {
         if ((button != null) && (button.image != null) && (button.image.sprite != null))
         {
             Debug.Log("Button image/sprite = " + button.image.sprite.name);
             
             Sprite buttonSprite = button.image.sprite;
 
             // do other stuff
         }
     }
 }

You will of course have to set each button OnClick() event in the inspector like so

alt text Here I attached ButtonHandler.cs to the canvas with all the buttons. Then on each button I first selected the Canvas. Then I selected the ButtonHandler.ButtonPressed() function. Then I selected the Button as shown in the image (in this case it was "Button (3)").

When the button is pressed the button is passed to the function where you can retrieve the sprite and do what you want with it.

If you still have a need to use a list for some reason you can do a look up based on the sprite name.


inspector.png (43.9 kB)
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 TBruce · Sep 03, 2016 at 07:07 PM 0
Share

@joelgarcia Did this work for you?

avatar image joelgarcia TBruce · Sep 04, 2016 at 12:54 AM 0
Share

Thanks. I did get this to work and didn't need my List after all. I also made use of PlayerPrefs to store the selection so that I could make use of the data I wanted in another scene.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Finding all the same strings in a List 2 Answers

How to load multiple sprites at runtime from List? 1 Answer

[ANSWERED] IndexOf and LastIndexOf both returning -1 when an item is on the list. 1 Answer

Total war Style Movement System with Right Click 0 Answers

How can I check value stored in a list when pressing button with the same name? 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