List check argument of item

Hi, i’m need simple inventory to store few stuff, it’s working, however i would like to use simpler method to check quantity of exaclty that item. Here is code:

public class Items
	{
		public string name;
		public int itemID;
		public int quantity;
	}
public List<Items> inventory = new List<Items>();
//set up few items
//add quantity to existing item
public void AddItem(int idForItem)
	{
		if(inventory.Exists(x => x.itemID == idForItem))
		{
			foreach(Items i in inventory)
			{
				if(i.itemID == idForItem)
					i.quantity ++;
			}
        }
    }

But checking whole items is no needed, I know which item i wanna check and I know it’s exist already in inventory so would like to use something simple like this:

inventory.itemID(0).quantity ++;

but it’s not working like that.

You should use the Dictionary.

Keep item id as key and item object as value:

Dictionary<int, Item> inventory = new Dictionary<int, Item>();

then you can simply get the item by the id rally fast:

if(inventory.ContainsKey(item_id))
   inventory[item_id].quantity ++;

That is because you’re accessing an integer (itemID) as if it is a list. However your list is called ‘inventory’.

Therefore the correct syntax would be

inventory(0).quantity++;

However, then it will not be checking what the itemID is and therefore won’t solve your problem.

Before going any further from here I would like to ask why the position in the list can’t be the items ID? So for example, the first item would have the ID of ‘0’.

Sorry but i want ask you for one more thing :slight_smile: how to set my int in other script based on an item’s quantity?
Something like;

private int number;
 //somewhere in code
 number = my_Inventory(item_id).quantity;
number = my_inventory(0).quantity;