How to stack items into your inventory? unsolved

100+ views, unsolved.

inventory is a list of items being populated from a chest loot. all items i am putting into it would be stackable, but how would one increase the number of an item than keep putting another of same type into that inventory? i know it would need a for loop. and a method to state if same item. like the item name or item id. it deals with increasing a number in the stack. I am not sure how to begin to do that in C#.

Dictionary doesn’t fit what i, cause that only creates one instance not more than one. if i am wrong can someone tell me differently. When i tried that all it did for me. Wanted stacking like quantity 1 to 99. 2 or more stacks in the inventory. how do i tell if one list to another that item exists to stack it, than if that item has reached max quantity to begin another.

	List<Item> loot = new List<Item>();
	List<Item> inventory = new List<Item>()
	
	void Start()
	{
		loot.Add(ItemGenerator.AddMeleeWeapon(ItemsNames.Sword, 10));
		loot.Add(ItemGenerator.AddShield(ItemsNames.LargeShield, 5));
	}
	
	public void LootWindow()
	{
		int cnt = 0;
		
		for(int y = 0; y < 5; y++)
			{
			for(int x = 0; x < 1; x++)
			{
				if(cnt < loot.Count)
				{					
					if(GUI.Button(new Rect(10 + ( x * 50 ), 20 + ( y * 50 + y * 5 ), 190, 50), loot[cnt].quantity)
					{				
						if(!inventory.Contains(loot[cnt]))
							inventory.Add(loot[cnt]);
						else
							loot[cnt].quantity += loot[cnt].quantity;
						
						loot.RemoveAt(cnt);
					}
				}
				{
					GUI.Button(new Rect(10  + (x * 50), 25 + y * 50, 190, 50), (x + y * 1).ToString());
				}
				
				cnt++;
			}
		}
	}
					
	public void InventoryWindow()
	{
		for(int i = 0; i < inventory.Count; i++)
		{
			if(inventory_.quantity > inventory*.maxQuantity)*_

inventory_.quamtity = inventory*.maxQuantity;*_

_ GUI.Button(new Rect(10, 50, i * 50, 50, 50), inventory*.quantity);
}
}*

This works well. My item generator changes the quantity in the loot window, so that they are now showing stacked items. When i try a chest out with this and comment out the RemoveAt line in the loot window it stacks inventory fine. But when i add more than one chest, with RemoveAt commended out or not. When i get to next chest it creats a new slot in my inventory for the item. Why does it skip over the if(!inventory.Contains(loot[cnt])) when i get to the next chest? does the list create a new item when it repopulates the list from my chest script. the trimmed script below in comment doesn’t show it fully, but an idea.
Does anyone know why it wouldn’t see the item in next chest as the same sword as the first chest?
Beginner C#._

In C# a Dictionary is would what you’d probably want. Add an item to the dictionary and increase it if it already exists. See C# Dictionary Examples - Dot Net Perls