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 Commissar777 · Apr 09, 2020 at 02:51 PM · event-listener

AddListener in a loop only running once

Greetings.

I am quite a beginner at Unity and at C#, but that's actually the first time I need to ask a question. So I have this bit of code :

    public class InvWindow : MonoBehaviour
 {
     GameObject itemButton;
     Inventory playerInventory;
     
     private void Start()
     {
         itemButton = this.transform.Find("ItemButton").gameObject;
         playerInventory = this.transform.parent.parent.Find("MainBody").GetComponent<Inventory>();
         // Creation of the contents
         int margin = 0;
         foreach (string key in playerInventory.inventory.Keys) {
             GameObject instance = Instantiate(itemButton, this.transform); // create a entry
             Vector3 position = instance.transform.localPosition;
             // change game object name to make it unique _ DOES NOT FIX THE ISSUE
             instance.name = ("itemButton_" + margin);
             // These four lines move the button down each time one is created
             position.y =+ margin;
             instance.transform.localPosition = position;
             instance.SetActive(true);
             margin =+ 30;
             // Set appropriate name
             instance.transform.Find("Text").GetComponent<Text>().text = key;
             // Create button interaction
             instance.GetComponent<Button>().onClick.AddListener(() => ItemClicked(key));
         }
     }
     private void ItemClicked(string name)
     {
         Debug.Log("Brrrt! Clicked on " + name);
     }
 }

It is supposed to read from a HashTable, and create a functional inventory window by instantiating the prefab button "itemButton" for each item. Currently the table has two entries, and everything works fine, two buttons are created with the proper renaming. However the AddListener bit only works for the first button (the argument is passed correctly), the second one is not clickable at all.

I've seen similar problems on Internet, but nothing I tried fixed this. What am I doing wrong?

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
0

Answer by Commissar777 · Apr 11, 2020 at 04:29 PM

I see. This has been a great help.

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

123 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Where is the best place to use delegate call back without calling it in Update function? 1 Answer

Subscribing to an event via non-mono script. 0 Answers

Listen for clicks outside of unity application 0 Answers

Button AddListener during runtime pointing to another GameObject? 1 Answer

ArgumentException for UnityEvent 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