Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 drod7425 · Feb 11, 2015 at 05:31 PM · uivariabledynamicparameters

Dynamically Added Listener to UI Button Not Calling With Variable Parameters

I'm making a role-based avatar selector. First the player selects their role and then picks from a set of avatars dressed like that role. Here are some subclasses that I've made to construct the avatar sets:

 [Serializable]
 public class Avatar {
     public string name;
     public Sprite icon;
 }
 
 [Serializable]
 public class RoleAvatarSet {
     public Toggle avatarPanelToggle;
     public RectTransform avatarHolder;
     public Avatar[] avatars;
 }

 public RoleAvatarSet[] roleAvatarSets;

I've filled 4 roleAvatarSets in the inspector with a variable number of avatars in each (from 1 to as many as 8). Next I needed a way to dynamically load those into my UI:

 void Start() {
     LoadAvatars();
 }
 
 void LoadAvatars() {
     for(int i=0;i<roleAvatarSets.Length;i++) {
         for(int j=0;j<roleAvatarSets[i].avatars.Length;j++) {
             GameObject avatarSelectionButton = (GameObject)Instantiate(avatarPrefab);
             avatarSelectionButton.transform.SetParent(roleAvatarSets[i].avatarHolder, false);
             avatarSelectionButton.transform.FindChild("Image").GetComponent<Image>().sprite = roleAvatarSets[i].avatars[j].icon;
             avatarSelectionButton.GetComponent<Button>().onClick.AddListener(() => SelectAvatar(i, j));
         }
         roleAvatarSets[i].avatarPanelToggle.GetComponentInChildren<Image>().sprite = roleAvatarSets[i].avatars[0].icon;
     }
 }
 
 public void SelectAvatar(int roleSet, int avatar) {
     Debug.Log(roleSet + " : " + avatar);
     selectedAvatar = roleAvatarSets[roleSet].avatars[avatar];
     roleAvatarSets[roleSet].avatarPanelToggle.GetComponentInChildren<Image>().sprite = roleAvatarSets[roleSet].avatars[avatar].icon;
 }

And it all loads up nicely. The problem is when I click one of the dynamically added avatar selection buttons, I get an IndexOutOfRangeException. When I debug what indexes the button is calling with, this is no surprise. Every one - no matter which button I click - calls SelectAvatar with a 4 for 'roleSet' and a 5 for 'avatar'. I only have 4 role sets, so 3 should be my highest index. And I have no clue why avatar is a 5.

I've also debugged my LoadAvatars method and everything checks out with the for loop indexes. It's like something gets messed up with AddListener. Am I using it incorrectly? Am I missing something? Any help would be appreciated.

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

1 Reply

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

Answer by Alejandro Orpi · Feb 11, 2015 at 05:45 PM

You are having this problem http://answers.unity3d.com/questions/791573/46-ui-how-to-apply-onclick-handler-for-button-gene.html

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 drod7425 · Feb 11, 2015 at 06:04 PM 0
Share

Yes, awesome. I suspected that was the issue. Thank you.

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

20 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

Related Questions

Speed won't work as a parameter 1 Answer

How to connect a variable to a UI text object 0 Answers

ScrollRect expand up to a max height 0 Answers

Set an Int variable to an animator parameter 1 Answer

Method to apply a UI Mask to Instantiated objects 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