• 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
0
Question by Hamsterlings · Mar 23, 2015 at 11:59 AM · for-loop

For loop with UI buttons alternative in new UI?

In an old project I had old UI and I had an loop that went through around 30 buttons. Each button had its own index that I used to do some action.

 for (var i = 2; i<=30; i++) {
 
     if (GUI.Button(Rect((28.2/100)*width, offset+(perc_btnY/100)*height , 80, 50), "+10")){ //Buy btn
     // tons of code that uses index of this button
         }
     }

So if button had index of 5, I would do something in some array at position 5 in other script, thus giving me freedom to use only that for loop for 30 buttons and get unique behavior for each of them.

Is there an alternative for new UI ? I know that I can instantiate buttons on canvas but can I somehow replicate all my functionality with one for loop?

What I need to do in this example :

 // pseudo code 
 
 for (var i = 2; i<=30; i++) {
  
      // if user clicks on this button
        InfoClass.Something.SomeList[indexOfThisButton].Remove(5);
      } 
 

I don't like idea of having one script for each button that would hold an index variable and control button. Somehow idea of having 30 scripts on one UI element doesn't seem right.

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

2 Replies

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

Answer by drod7425 · Mar 23, 2015 at 12:49 PM

With the new UI, you don't need to constantly check if a button is being clicked. Instead, you add OnClick listeners to the button:

 public Button[] buttons;
 
 void Start () {
     for(int i = 0; i < buttons.Length;i++ ) {
         int capturedIterator = i;
         buttons[i].onClick.AddListener(() => DoSomething(capturedIterator));
     }
 }
 
 public void DoSomething(int index) {
     
 }

I haven't tested that code, but it should be something generally like this. Also, make sure you are using UnityEngine.UI.

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 YoungDeveloper · Mar 23, 2015 at 12:41 PM

Create the buttons in scene or generate them via script and store them in array/list - public inspector array.

If you are adding buttons from inspector:

 //pseudocode
 
 //using UnityEngine.UI;
 
 public Image[] buttons;
 
 for(int i = 0; i < buttons.Length; i++){
     Debug.Log(buttons[i].gameObject.name);
 }

 //if you want to remove the elements
 for(int i = buttons.Length-1; i >= 0 ; i--){
     Destroy(buttons[i].gameObject);
 }

Comment
Add comment · Show 3 · 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 Hamsterlings · Mar 23, 2015 at 12:48 PM 0
Share

Thanks for reply!

$$anonymous$$y initial plan was along those lines as well.

How can I tell which button did user press without assigning new script to that button though ?

I know that I can create script and use lambda expression, for example, to find the button in list, but that means that I will have 30 buttons with 30 scripts.

Whole point of code in my post is to use one for loop to create and recognize which button was pressed by index.

avatar image YoungDeveloper · Mar 23, 2015 at 01:07 PM 0
Share

Yesterday i created same thing for the project you are asking. I had buttons in a scroll list, in user specified amount. $$anonymous$$y setup was that all those buttons were a child under one parent. Each button contains a class (inherited from mono) which can be added on gameobject. When i generate button in for loop i getcomponent of that class and set i index for that button. Each button also contains click event, when that is clicked i run method wich has no params, that method is located in the same script i store it's button index. From that point i launch buttons manager button and simply pass that index to know which of all i pressed.

avatar image drod7425 · Mar 23, 2015 at 01:17 PM 0
Share

You can do what you are asking about with my answer. Just put Debug.Log(index); in the DoSomething method.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

for in loop in Unity iPhone? 2 Answers

For loop on collision (two hit kill) 2 Answers

UnityScript Variable Scope? 2 Answers

Instantiate From array, but cycle trough each gameobject 1 Answer

For loop resetting itself, but needs to stop 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