StockableItem Add(StockableItem item, int number = 1) { int tempItem; if (inventory.TryGetValue(item, out tempItem)) { inventory[item] = tempItem + number; inventorySlots[inventory.Count - 1].text = item.GetName() + " : " + (tempItem + number); } else { inventory.Add(item, number); inventorySlots[inventory.Count - 1].text = item.GetName() + " : " + number; } return(item); }
// Start is called before the first frame update void Start() { _singletonInventory = this; inventory = new Dictionary <StockableItem, int>(); StockableItem item = new StockableItem("Torchlight"); inventoryUI.SetActive(false); foreach (var slot in inventorySlots) { slot.text = ""; } Add(item, 2); Remove(item); Add(item, 2); StockableItem coin = new StockableItem("Coin"); Add(coin); }
StockableItem Remove(StockableItem item, int number = 1) { int tempItem; if (inventory.TryGetValue(item, out tempItem)) { if (tempItem - number > 0) { inventory[item] -= number; inventorySlots[inventory.Count - 1].text = item.GetName() + " : " + (tempItem - number); } else { inventory.Remove(item); inventorySlots[inventory.Count - 1].text = ""; } } else { } return(item); }