Hey guys I have a inventory system that takes in item classes, the current way I have implemented it is to have the amount of items I can store by weight (Omitted unnecessary code):
public class Inventory
public Dictionary<item, int> inventory = new Dictionary<item, int>();
public void InventoryAdd(Item item)
inventory.Add(entity, inventory[item] += 1);
public void InventoryRemove(Item item, int amount)
if(inventory.ContainsKey(item) && amount <= inventory[item])
inventory[item] -= amount;
//Don't NEED this but I am just removing the item for now. I need to check the cost trade offs
if (inventory[item] == 0)
This is what I have implemented, but it's not really what I am looking for. I don't really like the idea of removing from a dictionary every time the user sets it to 0. I could keep a 0 index in the dictionary and check on the other side if it's greater and only display when greater, but that could leave me open to other problems seeing how I don't know how many items I am going to have seeing how people can craft their own items.
So I am torn between removing a key/value or having multiple inventories (Unlimited) with (Unlimited) empty indexes.
Any suggestions on how I could make this better? Or any suggestions on what you would do instead? Always looking to improve,
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
64 People are following this question.
Inventory Drop Function Problem
How to start an inventory system?
GUI focus control - inventory system issue
Toggle Inventory script
Best way to implement RPG Inventory System (w/ Sized Items)