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
4
Question by Rewpparo · Feb 28, 2015 at 11:36 AM · uibuttonlistener

Button.OnClic.AddListener(delegate{method(object);}), wrong object sent

Hi ! I have an array of object of type Station, and I'd like to generate one button per item, then when the button is clicked I want to do something with the corresponding Station object. Here's a simplified version of relevant code :

 foreach(Station s in stations)
 {
    GameObject b = Instantiate(buttonTemplate) as GameObject;
    //...
    Button b2 = b.GetComponent<Button>();
    b2.onClick.AddListener(delegate {setStation(s);});
 }

 //and further down
 public void setStation(Station s)
 {
    Debug.Log(s.name);
 }

No matter what button I clic, setStation is always called with the last element of the array, not the one corresponding to the button I clicked.

Why is this happening, and what is the correct way to get the result I want ? Thanks for help !

Comment
Add comment · Show 3
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 Rewpparo · Feb 28, 2015 at 11:39 AM 0
Share

Thanks that fixed it ! Can you sugest some reading so I can understand why ?

avatar image hexagonius · Feb 28, 2015 at 11:43 AM 1
Share

It's called a closure. A quick look found me this page here:

http://csharpindepth.com/Articles/Chapter5/Closures.aspx

avatar image Rewpparo · Feb 28, 2015 at 04:30 PM 0
Share

Thanks for your help !

1 Reply

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

Answer by hexagonius · Feb 28, 2015 at 11:30 AM

Create a local Station variable in the foreach and pass it into the setStation() method.

Comment
Add comment · Show 4 · 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 lukedd3 · Jul 26, 2016 at 04:20 PM 0
Share

Could you explain why we have to do it this way?

avatar image Bunny83 lukedd3 · Jul 26, 2016 at 09:28 PM 2
Share

Just read this blog post of Eric Lippert. Note that Eric is one of those who developed and designed the C# compiler for $$anonymous$$icrosoft. Also note that "C# 5" doesn't apply to Unity / $$anonymous$$ono in general.

This is a quite common problem and got asked several times before, see my answer over here.

avatar image lukedd3 Bunny83 · Jul 27, 2016 at 12:59 PM 0
Share

Thank you.

avatar image Nikunj-Kareliya · Oct 08, 2018 at 03:42 PM 0
Share

I had a same problem. but your solution is worked. Thanks, mate.

I up-voted this answer.

Cheers

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AddListener function not working? 6 Answers

button.onClick.AddListener(method); NOT Working 1 Answer

New UI Button Dynamic onClick Listener problem 1 Answer

Weird behaviour when trying to dynamically create buttons 0 Answers

Remove Listener from Button via 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