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 /
  • Help Room /
avatar image
0
Question by DrunkBearzz · Sep 20, 2016 at 06:09 AM · listdictionarylisteners

Add listener on Objects that are part of dictionary

 void ConfigButtonListeners()
     {
         buttonKeyTypes[Profile.keyType.LEFT] = buttonKeyLeft;
         buttonKeyTypes[Profile.keyType.RIGHT] = buttonKeyRight;
         buttonKeyTypes[Profile.keyType.JUMP] = buttonKeyJump;
         buttonKeyTypes[Profile.keyType.SHOT] = buttonKeyShot;
         buttonKeyTypes[Profile.keyType.PICKUP] = buttonKeyPickup;
 
         foreach(KeyValuePair<Profile.keyType,GameObject> pair in buttonKeyTypes) {
             Button button = pair.Value.GetComponent<Button>();
             Debug.Log(pair.Key);        //gives right value
             button.onClick.AddListener(
               delegate {
                   isConfiguring = true;
                    Debug.Log(pair.Key);  //gives wrongvalue
                   configButtonPointer = pair.Value;
               });
         }
         
     }


  1. Inserting all public GameObjects in dictionary (works correctly, I checked the values)

  2. For each dictionary value(GameObject) I'm getting it's Button component

  3. I'm defining what will happen onClick on every of those buttons over delegates (Problem is here somewhere)

  4. When I click on one of those buttons 'configButtonPointer ' will refrence to that object that was clicked

The problem is that 'configButtonPointer' is always refrencing last value(GameObject) in dictionary which is buttonKeyPickup, instead of being refrence to a GameObject that holds button that was last pressed. I hope you understand what I was trying to say and thanks in advance.

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 DrunkBearzz · Sep 20, 2016 at 09:31 AM

UPDATE

I changed configButtonPointer = pair.Value; to configButtonPointer = button.transform.gameObject.transform.parent.gameObject; and now it works. I guess it has something to do with a scope of delegate but if anyone knows why it didn't work before, explanation would be nice.

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

70 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

Related Questions

Getting "object reference not set" error when creating a list dictionary. 1 Answer

How to create a turn based system manager? 0 Answers

Enemy Database using list/dictionary to store stats (C#)? 1 Answer

Insert string into empty list at a specific index 0 Answers

Need help to generate a random assortment of 4 buttons - C# 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