Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 IamAnewUser · May 24, 2018 at 04:43 PM · variablebuttonslistenerlisteners

AddListener with argument on multiple Buttons

This has been bugging me too long so I thought I get your advice ... I'm trying to add listeners to many button Components and with arguments.

 string[] saveFiles = Directory.GetFiles(Application.persistentDataPath + "/saves/", "*.dat");
 GameObject curButton;
 string curName;
 
 for(int i = 0; i < saveFiles.Length && i < loadUI.transform.childCount - 1; i++) {
      curName = Path.GetFileName(saveFiles[i]);
      curName = curName.Substring(0, curName.Length - 4);
 
      curButton = loadUI.transform.GetChild(i + 1).gameObject;
      curButton.GetComponent<Button>().onClick.AddListener(delegate { ClickToLoad(curName); });
      curButton.GetComponentInChildren<Text>().text = curName;
  }

The problem is with the curName variable. If I change the variable it will change for every LIstener. For example if I put this :

 curName = "test";

at the end of my code then no matter what button I click(that has a listener assigned with this code) the ClickToLoad()'s argument will always be "test".

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 IamAnewUser · May 24, 2018 at 04:46 PM 0
Share

A workaround might be declaring variables dynamically but I don't know how to do this either. I'll search more for a solution in delegates and come back

avatar image PizzaPie · May 24, 2018 at 10:26 PM 0
Share

To make it work, as it is, you need to declare the curName variable inside the loop scope

 for(//...){
          string curName = //...
          //...
 }

, so it creates a new object on every iteration which the anonymous methods later gonna associated with and take as the argument if it is outside of the loop the object will be the same (string) and will have the the last assigned value.

That is pretty much what happens in the solution you provide with the function.

Cheers.

1 Reply

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

Answer by IamAnewUser · May 24, 2018 at 04:54 PM

I found a solution (it came to me a minute after I posted this, sigh) It's a hack but it works

I made a method like this

 void Try(GameObject curButton, string curNameT)
      curButton.GetComponent<Button>().onClick.AddListener(delegate {ClickToLoad(curNameT); });
 }

and I replaced this line in line 10 of my question's code

 curButton.GetComponent<Button>().onClick.AddListener(delegate {ClickToLoad(curName); });

with this

 Try(curButton, curName);
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

91 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

Related Questions

How to access the persistent listener gameobjects on a button? 1 Answer

Is there a way to get the Argument of a Listener? 0 Answers

Check if a listener has already been added to a button? 2 Answers

Do we need to remove button click listener added from inspector? 0 Answers

Change variable in Mono from other script 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