- Home /
Create a button from an int, remove button when clicked, and never load it again
I am trying to make a new button based off the number of dungeons the player has beaten in the game. The more dungeons the player beat, the more items they can buy. I'm trying to make it based off of a basic Item class where there is a number to tell when the item was unlocked. When the player buys this item, I want the button destroyed and the element to be removed from the list so that, when the player moves to a different location and returns to the shop, the item can't be bought again.
I made it this way first:
public ItemClass[] allBlacksmith;
public List<ItemClass> availableBlacksmith;
void Start(){
foreach (ItemClass smith in allBlacksmith)
{
if (smith.dungeonUnlock <= PlayerStatManager.dungeonsBeaten)
{
Debug.Log("here");
//availableBlackSmith.Add(smith);
//create the button within the blacksmith shop panel
if (smith.itemTypes == ItemClass.ItemType.Accessory)
{
itemNameDisplay.text = smith.itemName + "\n" + smith.cost + " Gold. Vitality +" + smith.vitInc;
}
else if (smith.itemTypes == ItemClass.ItemType.Armor)
{
itemNameDisplay.text = smith.itemName + "\n" + smith.cost + " Gold. Defense +" + smith.defenseInc;
}
else if (smith.itemTypes == ItemClass.ItemType.Weapon)
{
itemNameDisplay.text = smith.itemName + "\n" + smith.cost + " Gold. Attack +" + smith.attackInc;
}
RectTransform itemTransform = ((GameObject)Instantiate(itemButton)).GetComponent<RectTransform>();
itemTransform.name = smith.itemName;
itemTransform.GetComponent<Button>().onClick.AddListener(() =>
{
if (PlayerStatManager.gold >= smith.cost)
{
//ask the player if they want to buy ITEMNAME for COST
//subract the cost
PlayerStatManager.gold -= smith.cost;
//add the increase of the item
PlayerStatManager.attack += smith.attackInc;
PlayerStatManager.defense += smith.defenseInc;
PlayerStatManager.vitality += smith.vitInc;
PlayerStatManager.level++;
//destroy the object
Destroy(itemTransform.gameObject);
}
//update the text
playerStatTextManager.attack.text = "Attack: " + PlayerStatManager.attack;
playerStatTextManager.defense.text = "Defense: " + PlayerStatManager.defense;
playerStatTextManager.vitality.text = "Vitality: " + PlayerStatManager.vitality;
teriaCanvasManager.playerGold.text = "Gold: " + PlayerStatManager.gold;
//check the player's health again
PlayerStatManager.convHealth = Mathf.Pow(PlayerStatManager.vitality, 1.3f) * 3.1; ;
int healthBefore = PlayerStatManager.totalHealth;
PlayerStatManager.totalHealth = (int)PlayerStatManager.convHealth;
int healthAfter = PlayerStatManager.totalHealth;
PlayerStatManager.currentHealth += healthAfter - healthBefore;
teriaCanvasManager.playerHP.text = "HP: " + PlayerStatManager.currentHealth + "/" +
PlayerStatManager.totalHealth;
//save game data
SaveAllData.saveAllData();
});
itemTransform.SetParent(blacksmithPanel);
}
}
}
That worked, but I couldn't get rid of the button for the remainder of the game. Every time the shop was revisited, the button, logically, would appear again. How can I prevent this?
Answer by Laiken · Jun 21, 2017 at 06:39 PM
I'm not sure if I understood corretly, but if I did, when you click the button, "smith.dungeonUnlock" should be incremented and saved (if not, everytime you reenter, dungeonUnlock will be lower than dungeonsBeaten and the button will be recreated).
Your answer
Follow this Question
Related Questions
Button list from array 4 Answers
Looping through Button Array and checking for OnClick 0 Answers
No Movement when Attempting to Lerp through an array 2 Answers
Button showing when array is full 1 Answer
A node in a childnode? 1 Answer