• 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 kayneplays · Jan 26, 2017 at 03:12 PM · c#unity 5buttonimagemultiple

Button calls itself numerous times

Hello,

I am currently working on a game and I am trying to create a buff slot system with 3 buttons and a buff shop with 7 buttons. What I want to do is every time a buff slot button is clicked, it will open the buff shop panel and when the user clicks on one of the 7 buffs in the panel, the buff button's image will transfer into the buff slot button.

I have implemented just about everyt$$anonymous$$ng so when I click the first buff slot and select a buff, the buff slot's image will change to the buff selected. But when I click on the second buff slot and select another buff, it will replace the image on both buttons. And then the t$$anonymous$$rd one will do the same for all 3 buttons. Almost like recursively calling itself when I click on other buttons. How can I avoid the buttons from changing the other buttons' images? I am fairly new to Unity/C# and I would appreciate any suggestion I can get.

EDIT: I completely forgot to show my code:

         //T$$anonymous$$s is under Start()
         buffSlot_1.GetComponent<Button>();
         buffSlot_1.onClick.AddListener(() => addBuff(buffSlot_1));
 
         buffSlot_2.GetComponent<Button>();
         buffSlot_2.onClick.AddListener(() => addBuff(buffSlot_2));
 
         buffSlot_3.GetComponent<Button>();
         buffSlot_3.onClick.AddListener(() => addBuff(buffSlot_3));


And t$$anonymous$$s is where addBuff runs:

 void addBuff(Button button) {
 
         buffPanel.GetComponent<GameObject>();
         buffPanel.SetActive(true);
 
         damageBoostButton.GetComponent<Button>();
         damageBoostButton.onClick.AddListener(() => {
             Debug.Log("Gain a damage boost!");
             dmgBool = true;
             buffPanel.SetActive(false);
             button.interactable = false;
             damageBoostButton.interactable = false;
             button.image.overrideSprite = damageBoostButton.image.sprite;
         });
 
         defenseBoostButton.GetComponent<Button>();
         defenseBoostButton.onClick.AddListener(() => {
             Debug.Log("Gain a defense boost!");
             defBool = true;
             buffPanel.SetActive(false);
             button.interactable = false;
             defenseBoostButton.interactable = false;
             button.image.overrideSprite = defenseBoostButton.image.sprite;
         });
 
         critBoostButton.GetComponent<Button>();
         critBoostButton.onClick.AddListener(() => {
             Debug.Log("Crit Boost");
             critBool = true;
             buffPanel.SetActive(false);
             button.interactable = false;
             critBoostButton.interactable = false;
             button.image.overrideSprite = critBoostButton.image.sprite;
         });
 
         hasteBoostButton.GetComponent<Button>();
         hasteBoostButton.onClick.AddListener(() => {
             Debug.Log("Haste Boost");
             hasteBool = true;
             buffPanel.SetActive(false);
             button.interactable = false;
             hasteBoostButton.interactable = false;
             button.image.overrideSprite = hasteBoostButton.image.sprite;
         });
 
         iceSpikeButton.GetComponent<Button>();
         iceSpikeButton.onClick.AddListener(() => {
             Debug.Log("Increase Ice Spike");
             iceSpikeBool = true;
             buffPanel.SetActive(false);
             button.interactable = false;
             iceSpikeButton.interactable = false;
             button.image.overrideSprite = iceSpikeButton.image.sprite;
         });
 
         iceWallButton.GetComponent<Button>();
         iceWallButton.onClick.AddListener(() => {
             Debug.Log("Increase Ice Wall");
             iceWallBool = true;
             buffPanel.SetActive(false);
             button.interactable = false;
             iceWallButton.interactable = false;
             button.image.overrideSprite = iceWallButton.image.sprite;
         });
 
         healthRegenButton.GetComponent<Button>();
         healthRegenButton.onClick.AddListener(() => {
             Debug.Log("HPRegen Boost");
             hpRegenBool = true;
             buffPanel.SetActive(false);
             button.interactable = false;
             healthRegenButton.interactable = false;
            // button.image.overrideSprite = healthRegenButton.image.sprite;
         });
     }

Thanks!

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
1

Answer by MM_Games · Aug 18, 2022 at 08:00 PM

Please note that onClick.AddListener accumulates on itself, meaning if you are using start method to assign the listener for example and the script holding the start is present more than once; t$$anonymous$$s will result in "every addListener" will be added, leading to your question title

Button calls itself numerous times

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.

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

99 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

Related Questions

Image not appearing after transferring build to Mac from PC? 0 Answers

how to make a gaze work with a button? 0 Answers

Someone please help me with combination lock 0 Answers

How to catch events from scripted buttons 1 Answer

Adding buttons as components not working 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