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!