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 /
avatar image
0
Question by MrAwesome343 · Jun 21, 2017 at 06:25 PM · buttonarraylistdestroyloop

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?

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 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).

Comment
Add comment · Show 1 · 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
avatar image MrAwesome343 · Jun 21, 2017 at 10:34 PM 0
Share

Yes, and I need the button to stay gone

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

85 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

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


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