Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by Jake45365 · Jun 11, 2015 at 10:33 AM · uigameobjectbuttonloopbutton trigger events

Issue with spawning buttons and assigning listeners

So, I'm creating a bunch of buttons and trying to give them all the same method on click but it should be different based on the key each button possesses. However, they all call the exact same key (which is the last one added). I am very confused at what's going on or why they all use the same same string. Anyone know what's going on with adding listeners in a for loop? Here's the code I call in start

public void populateVisibleNames() {

 for(int i = 0; i<visibleNames.Count; i++)
 {
     Transform newButton = Instantiate(namePrefabButton) as Transform;
     newButton.SetParent(buttonPanel.transform);
     Button b = newButton.GetComponent<Button>();
     b.onClick.AddListener(() => disablePanel(nameButton, visibleNames[i-1].ToString()));
     newButton.GetChild (0).GetComponent<Text>().text = visibleNames[i].ToString();
     nameButtons.Add(b);
     newButton.gameObject.SetActive(true);
     newButton.transform.localPosition = new Vector3(0,-i*30,0);
 }

}

Comment
Add comment · Show 2
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 Jake45365 · Jun 11, 2015 at 06:54 PM 0
Share

Thanks, not sure why you can't do it directly, but it worked. Thanks a bunch

avatar image digzou · Jun 12, 2015 at 05:47 AM 0
Share

Converting it to an answer. please approve it. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by digzou · Jun 11, 2015 at 12:30 PM

I was facing some similar issues recently. This is how I solved it. Save the name to a local variable and the use that variable.

      for(int i = 0; i<visibleNames.Count; i++)
      {
          Transform newButton = Instantiate(namePrefabButton) as Transform;
          newButton.SetParent(buttonPanel.transform);
          Button b = newButton.GetComponent<Button>();
 
          string btn_name = visibleNames[i-1].ToString();         
 
          b.onClick.AddListener(() => disablePanel(nameButton, btn_name));
          newButton.GetChild (0).GetComponent<Text>().text = visibleNames[i].ToString();
          nameButtons.Add(b);
          newButton.gameObject.SetActive(true);
          newButton.transform.localPosition = new Vector3(0,-i*30,0);
      }


Here's the snippet if my code if it helps you by any chance :

 if(!languagesSet){
         foreach(LanguagePack lp in languageList){
             
             GameObject langbtn = Instantiate(Resources.Load<GameObject>("LangButton"));
             langbtn.transform.SetParent(GameObject.Find("VerticalPanel").transform);
             langbtn.name = lp.Id;
             langbtn.GetComponent<RectTransform>().localScale = Vector3.one;
             langbtn.GetComponentInChildren<UnityEngine.UI.Text>().text = lp.Id;
             string str = lp.Id;
             langbtn.GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() => { changeLanguage(str);});
         languagesSet=true;
         }
         }
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Children of DualTouchControls prefab keep being forced to active 0 Answers

UI button mobile - want a button drag to register as a click 0 Answers

How to detect button presses and change child object text in the button's Parent Object script 0 Answers

Can't to get Button script from gameobject in unity 0 Answers

4.6 UI Check if a button and a in-game object are overlapping 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