- Home /
Inventory System: How to detect equal items on a list for increasing amount?
I've this problem, i need to add an item on a slot, but i want the code to verify if the item that i'm adding already exist on the slot vector.
I have a separate list for every type of item in my inventory like this:
 private List <Item> weapons;
 private List <Item> armor;
 private List <Item> consumables;
 private List <Item> etc;
 
Then i have this slot vector:
 Consumable_Slot[] consumable_slots;
The consumable slot is a class that holds the Item from the inventory with these values:
 private Item consumable_on_slot;
 private int consumable_amount;
 private int consumable_max_amount;
i already can add an item on a slot, but i cant get how to stack items by just increasing the amount, i tried this code but it does not work:
 for (int i = 1; i < consumables_slots.Length; i++)
         {
             if (i < inventory_reference.Get_Consumables_List ().Count)
             {
                 if(consumables_slots[i-1] == null)
                 {
                     consumables_slots[i-1].Add_Consumable_To_Slot(inventory_reference.Get_Consumables_List()[i-1]);
                 }
                 else if(inventory_reference.Get_Consumable_On_List(i).Get_Item_ID() == consumables_slots[i-1].Get_Consumable_on_Slot().Get_Item_ID())
                 {
                     consumables_slots[i-1].Set_Consumable_Current_Amount();
                 }
                 else
                 {
                     consumables_slots[i].Add_Consumable_To_Slot(inventory_reference.Get_Consumables_List()[i]);
                 }
             }
         }
this is how i add the item on the slot:
 public void Add_Consumable_To_Slot (Item new_consumable)
     {
         this.consumable_on_slot = new_consumable;
         consumable_max_amount = consumable_on_slot.Get_Max_Amount ();
         consumable_amount = 1;
         text_amount_reference.text = consumable_amount.ToString ();
         image_reference.sprite = consumable_on_slot.Get_Item_Icon ();
         image_reference.enabled = true;
     }
 
Great free pack Here that has a lot of example of this if you wanna check it out. its quite old but still valid for what you are looking to do.
Edit:
Can Confirm it works on newer Unity's too i use it regularly to fast track editor window builds.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                