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 /
avatar image
0
Question by RealMTG · Feb 03, 2016 at 06:22 PM · buttonarray

Adding listener to buttons through script results in array out of index

Hi

I have a function that creates a few buttons that need to be generated at runtime and I then need to assign their click event by a script. Now this doesn't work. I assign them a function that needs an int. The code I use for this is as simple as this:

 for (int i = 0; i < tabs.Length; i++)
 {
     Button newButton = Instantiate(tabButton);
     newButton.transform.SetParent(tabButtonsHolder);
     newButton.onClick.AddListener(() => { ChangeTab(i); });
 }

Now this doesn't work. Whenever I click the button I just get an Array index out of range error. I also have a debug.log message that displays the index number that was given to the button. So I have 4 objects in an array so the last object has the index of 3. And for some reason, EVERY button has the index of 4. It always goes one number above.

I have no idea what is going on. Any help with this is greatly appreciated!

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 vintar · Feb 03, 2016 at 06:27 PM 0
Share

Seems like you are adding 4 listeners to one button in your example. I would assume you need to create the new button inside of the loop.

avatar image RealMTG vintar · Feb 03, 2016 at 06:43 PM 0
Share

Yes, I create a new button for each loop. I forgot to include that. Sorry.

1 Reply

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

Answer by dppc · Feb 03, 2016 at 07:54 PM

You're running into variable capturing.

To avoid that, replace the last line of your loop with these two lines:

 int n = i;
 newButton.onClick.AddListener(() => { ChangeTab(n); });
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 RealMTG · Feb 03, 2016 at 08:00 PM 0
Share

Thanks a ton! This worked!

avatar image AhmedGalal1987 · Oct 23, 2016 at 12:26 PM 0
Share

Thank you! Very helpful :)

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

38 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

Related Questions

Call a variable from each game object in an array? 1 Answer

Changing texture of pressed button in array 1 Answer

Button array c# 0 Answers

reference array item relative to selectiongrid button 0 Answers

How to make a or array of GUI.Button's? 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