Question by 
               ChocoboScribe · Oct 19, 2020 at 09:32 AM · 
                c#textupdateinventory system  
              
 
              Removing an item from Inventory and updating item count on HUD
This for an assignment I'm working on, I'm getting my inventory system set up and I'm having trouble figuring out how to properly remove the item from the inventory and for my stack able items (health potions) having the item count text to update as items are added or taken away.
I've attached the inventory and Inventory Display scripts and an image of the HUD from my game. Thanks!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Inventory : MonoBehaviour
 {     
     [SerializeField]
     // A List of Items
     private List<Item> inventory = new List<Item>();
 
     private void OnTriggerEnter2D(Collider2D pickUp)
     {
         // When we enter an object's trigger
         // intiialize an Item type Variable using the GetComponent function
         // if the object has the component it will be initialized otherwise it will remain null
         Item item = pickUp.GetComponent<Item>();
 
         // If the object has an Item Coponent
         if (item != null)
         {
             // Call the AddItem function with item as the arguement
             AddItem(item);
         }
     }
     //Adds item to inventory
     public void AddItem(Item newItem)
     {
         /*
          *Consumable items can be stacked
          *This means if the player has more than one of a spefific consumable item
          *only one invenyory slot will be taken. the itemCount will reflect how many of
          *the item the player has
          */
 
         if(newItem.ItemType == "Consumable")
         {
             // Loop through the list to se eif we already have the consumable
             for(int i = 0; i < inventory.Count; i++)
             {
                 // We have this item already
                 if(inventory[i].ItemName == newItem.ItemName)
                 {
                     // Add to the invenotry count
                     inventory[i].ItemCount += newItem.ItemCount;
                     Debug.Log("You added a " + newItem.ItemName + " to your inventory!");
                     Debug.Log("You now have " + inventory[i].ItemCount + " " + inventory[i].ItemName);
                     // Deactivate the game object so it cannot be picked up again
                     newItem.gameObject.SetActive(false);
                     // We found the object so return the function
                     return;
                 }
             }
             // We don't have this item yet
             // Add the new item to the inventory
             inventory.Add(newItem);
             Debug.Log("You added a " + newItem.ItemName + " to your inventory!");
             // Log the description
             Debug.Log(newItem.ItemDescription);
             // Deactivate the game object so it cannot be picked up again
             newItem.gameObject.SetActive(false);
         }
         /*
          * Weapons are not stackable. Each Weapon, even duplicates, take up their own inventory slot
          */
         else if(newItem.ItemType == "Weapon")
         {
             // Add the new item to the inventory
             inventory.Add(newItem);
             Debug.Log("You added a " + newItem.ItemName + " to your inventory!");
             // Log the description
             Debug.Log(newItem.ItemDescription);
             // Deactivate the game object so it cannot be picked up again
             newItem.gameObject.SetActive(false);
             // Let's print out the weapon stats. To access the inherited class members we need to cast as that object
             // check if the new item is a staff
             if(newItem is SagesStaff)
             {
                 // Cast newItem into a Staff type object
                 SagesStaff newStaff = newItem as SagesStaff;
                 // Log the Stats
                 Debug.Log("Weapon stats: ");
                 Debug.Log("Damage: " + newStaff.Damage);
                 Debug.Log("Strength: " + newStaff.Strength);
                 Debug.Log("Magic: " + newStaff.Magic);
             }
         }
         else if(newItem.ItemType == "Quest")
         {
             // Add the new item to the inventory
             inventory.Add(newItem);
             Debug.Log("You added a " + newItem.ItemName + " to your inventory!");
             // Log the description
             Debug.Log(newItem.ItemDescription);
             // Deactivate the game object so it cannot be picked up again
             newItem.gameObject.SetActive(false);
         }
     }
     // Remove items from Inventory
     // Removes Consumable Items from Inventory
     public void RemoveConsumableItem(Item item)
     {
         if(item.ItemName == "Weak Health Potion")
         {
             if(inventory.ItemCount <= 1)
             {
                 inventory[1].ItemCount -= item.ItemCount;
                 Debug.Log("Weak Health Potion removed from inventory.");
             }
             else
             {
                 Debug.Log("You don't have any Weak Health Potions...");
             }
             
         }
         if (item.ItemName == "Full Health Potion")
         {
             if(inventory.ItemCount <= 1)
             {
                 inventory[1].ItemCount -= item.ItemCount;
                 Debug.Log("Full Health Potion removed from inventory.");
             }
             else
             {
                 Debug.Log("You don't have any Full Health Potions...");
             }            
         }
     }
     // Removes Keys from Inventory
     public void RemoveKeyItem(Item key)
     {
         if(key.ItemName == "Bone Key")
         {
             Debug.Log("Bone Key removed from inventory.");
         }
         if (key.ItemName == "Water Key")
         {
             Debug.Log("Water Key removed from inventory.");
         }
         if (key.ItemName == "Fire Key")
         {
             Debug.Log("Fire Key removed from inventory.");
         }
         if (key.ItemName == "Thunder Key")
         {
             Debug.Log("Thunder Key removed from inventory.");
         }
     }
 
     // Checks is player has item
     public bool HasItem(string itemName)
     {
         // loop through the array
         for (int i = 0; i < inventory.Count; i++)
         {
             //If the inventory item name matches rhe itemName argeument
             if (inventory[i].ItemName == itemName)
             {
                 // retrn true value
                 return true;
             }
         }
         // Othwrwise, retrn false
         return false;
     }
 }
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class InventoryDisplay : MonoBehaviour
 {
     // Set Consumable Item Icons
     public Image halfHealthPotionIcon;
     public Image fullHealthPotionIcon;
 
     // Set Potion count text
     public Text halfHealthPotionCountText;
     public Text fullHealthPotionCountText;
 
     // Set Weapon Item Icon
     public Image weaponIcon;
     
     // Set the Quest Item Icon Variables
     public Image boneKeyIcon;
     public Image fireKeyIcon;
     public Image waterKeyIcon;
     public Image thunderKeyIcon;
     public Image bubbleCrystalIcon;
     public Image fireCrystalIcon;
     public Image cometCrystalIcon;
 
     // Set Invetory script
     private Inventory inventory;    
     
     // Start is called before the first frame update
     void Start()
     {
         // Call the script
         inventory = GameObject.Find("Player").GetComponent<Inventory>();
 
         // Call Potion count text
         halfHealthPotionCountText = GameObject.Find("HalfPotionCount").GetComponent<Text>();
         fullHealthPotionCountText = GameObject.Find("FullPotionCount").GetComponent<Text>();
 
         // Set potion count text to zero to start
         halfHealthPotionCountText.text = ": 0";
         fullHealthPotionCountText.text = ": 0";
 
         // Set wepaon item icon to inactive to start
         weaponIcon.gameObject.SetActive(false);
 
         // Set Quest Item icons to inactive to start
         // Key icons
         boneKeyIcon.gameObject.SetActive(false);
         fireKeyIcon.gameObject.SetActive(false);
         waterKeyIcon.gameObject.SetActive(false);
         thunderKeyIcon.gameObject.SetActive(false);
         // Crystal Icons
         bubbleCrystalIcon.gameObject.SetActive(false);
         fireCrystalIcon.gameObject.SetActive(false);
         cometCrystalIcon.gameObject.SetActive(false);
     }
 
     // Set inventory icons to active when item is picked up
     protected void ItemGet()
     {
         if(inventory.HasItem("Sage's Staff"))
         {
             weaponIcon.gameObject.SetActive(true);
         }
         if(inventory.HasItem("Bone Key"))
         {
             boneKeyIcon.gameObject.SetActive(true);
         }
         if (inventory.HasItem("Water Key"))
         {
             waterKeyIcon.gameObject.SetActive(true);
         }
         if (inventory.HasItem("Fire Key"))
         {
             fireKeyIcon.gameObject.SetActive(true);
         }
         if (inventory.HasItem("Thunder Key"))
         {
             thunderKeyIcon.gameObject.SetActive(true);
         }
         if (inventory.HasItem("Bubble Crystal"))
         {
             bubbleCrystalIcon.gameObject.SetActive(true);
         }
         if(inventory.HasItem("Fire Crystal"))
         {
             fireCrystalIcon.gameObject.SetActive(true);
         }
         if(inventory.HasItem("Comet Crystal"))
         {
             cometCrystalIcon.gameObject.SetActive(true);
         }
     }    
 }
 
                 
                hud.png 
                (105.7 kB) 
               
 
              
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                