• 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
Question by jasmyn · Sep 13, 2014 at 04:07 AM · button4.6unitygui

How to stop clones sharing listener?

Unity 4.6, scripting the buttons... Iterate through a directory and create a button for each file... The value of f.Name in the AddListener bit should be set independently for each button. Instead every button has the value of the last f.Name to be processed (the last file alphabetically in the directory). Any ideas?

 foreach (FileInfo f in fileInfo) {
     Button btnCurrLvl = (Button) Instantiate(btnLvl);
 
     btnCurrLvl.GetComponentInChildren<Text>().text = f.Name.Remove(f.Name.Length-5);
 
     Debug.Log(f.Name); // f.Name is different every time
     btnCurrLvl.name = f.Name; // renaming the btns works
     btnCurrLvl.GetComponent<Button>().onClick.AddListener(() => LoadLocalLvl(f.Name)); // all the listeners on all created buttons are set to the last value of f.Name!?
 }
Comment

People who like this

0 Show 1
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 EsoEs · Nov 11, 2014 at 05:28 AM 0
Share

Im having the same issue, have you fan a solution yet?

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Kiwasi · Nov 11, 2014 at 06:32 AM

Shameless self promotion: I've got a video tutorial here on this.

This is the nature of loops and variable scopes. To fix you simply need to declare a local scope variable.

 string tempName = f.Name;
 btnCurrLvl.GetComponent<Button>().onClick.AddListener(() => LoadLocalLvl(tempName)); 


Comment

People who like this

0 Show 3 · 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 Kiwasi · Nov 11, 2014 at 06:35 AM 0
Share

The longer explanation runs something like this:

f is declared outside of the loop. So it lasts longer then the loop does. Each iteration f changes. Your listener only holds a reference to the memory location of f. So a f changes in memory so does the listener.

By contrast my tempName is declared inside the loop. So it only lasts until the closing } of the loop. This means a new memory location is used for tempName each iteration through the loop. When your listener is called later the memory is correct.

avatar image jasmyn · May 04, 2015 at 12:12 AM 0
Share

Reference vs value, really should have remembered that one... Thanks. :)

avatar image Upfront_Applications · Nov 20, 2015 at 12:02 AM 0
Share

Hi, I tried this solution and it didn't work for me :/;

 foreach (Items it in ItemsToGenerate)
         {
             GameObject go = new GameObject() ;
             go.transform.SetParent (GlobalState.appController.searchScene.transform);
             go.tag = "Item";
             go.name =  it.itemName;
             go.transform.SetAsFirstSibling();
             int i = 0;
             if (!listView)
             {
                 Image image = go.AddComponent(typeof (Image)) as Image;
                 image.rectTransform.sizeDelta = new Vector2 ((Screen.width / maxColumns) - Screen.width * 0.03f, (Screen.width / maxColumns) - Screen.width * 0.03f);
                 image.rectTransform.pivot = new Vector2 (0f, image.rectTransform.rect.max.y * 0.02f);
                 //image.overrideSprite = it.image;
                 float spacer = column * (Screen.width *0.02f);
 
                 Vector3 slotPosition = new Vector3((Screen.width *0.02f) +(spacer) + (image.rectTransform.sizeDelta.x * column),
                                                    Screen.height - (Screen.height * 0.15f) - (image.rectTransform.sizeDelta.y * row),
                                                    0);
                 image.rectTransform.position  = slotPosition;
                 image.sprite = it.image[0];
 
                 Button b = go.AddComponent (typeof(Button)) as Button;
                 Items item = it;
                 b.onClick.AddListener (() => ItemListener(it));
 
                 if (column + 1 > maxColumns -1)
                 {
                     column = 0;
                     row ++;
                 }
 
                 else
                 {
                     column ++;
                 }
             }
 
             else
             {
 
             }
             i++;
         }
     }
 
     void ItemListener(Items it)
     {
         currentItem = it;
         ToItem();
         //GlobalState.DownloadItem(it.bundleLink);
     }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I create a button that toggles with the 4.6 UI? 6 Answers

Unity 4.6 GUI weird toggle+button interaction? 2 Answers

On Click paramaters disappear from button prefab? 5 Answers

Transition for buttons do not work before interaction 0 Answers

(4.6 UI) How to set up a window with x buttons, with a scroll bar? 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