Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 everything 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 third 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:

         //This 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 this 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

0 Replies

· Add your reply
  • Sort: 

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

98 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

Related Questions

Add an Icon next to a text inside a button Unity 5.6 1 Answer

How to use one button to play different audio for different Targets? 0 Answers

Can't access functions on my UI button 1 Answer

button function on unity5 1 Answer

Creating dynamically moving buttons/GUI objects. 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