When I add an item to my inventory in my game, if it already exists it is supposed to add on to the quantity but instead, it adds once and then doesn't change it afterwards. Where am I going wrong?
This is my inventory manager, for testing purposes, when I send the item into AddItem the quantity is always set to 1.
This is custom Inventory Item class
The ideal quantity outcome when I add the item in (Item has a fixed quanityt of 1 for testing) is:
0 1 2 3 4 5 6 7 8 9... etc
But instead it goes like this:
0 1 2 2 2 2 2 2 2 2... etc
Where am I going wrong?
If more info is needed, I can give more.
This is my inventory manager, for testing purposes, when I send the item into AddItem the quantity is always set to 1.
// Item = The item......int = the qauntity public List<InvItem> inventory = new List<InvItem>(); private const int MAX_ITEM_STACK = 30; private const int MAX_INV_SLOTS = 15; private const string UNABLE_TO_ADD_ERROR = "ERROR: Cannot add the item to the inventory. \nInventoryManager.cs(AddItem(Item item, int quantity)) Line 30"; public void AddItem(InvItem item) { // i is only here for testing, testing for qauntity adding // i is 0 so i will always get the first item cause i only have one item in the game so far int i = 0; if (item.item != null & item.quantity > 0) { if (ContainsItem (item)) { Debug.Log ("Item Q: " + item.quantity); inventory[i].Quantity += item.quantity; Debug.Log ("Inv Q: " + inventory[i].Quantity); } else { if (inventory.Count < MAX_INV_SLOTS) { inventory.Add (item); } } } else { Debug.LogError (UNABLE_TO_ADD_ERROR); } } private bool ContainsItem(InvItem item) { for (int i = 0; i < inventory.Count; i++) { if (inventory[i].item.id == item.item.id) { return true; } } return false; }
This is custom Inventory Item class
public class InvItem { public ItemType type = ItemType.Null; public Item item = null; public int quantity = 0; public int Quantity { get { return quantity; } set { quantity = value; } } public enum ItemType { Weapon, Armor, Consumable, Item, Null } public ItemType SetType(string type) { switch (type) { case "Weapon": return ItemType.Weapon; default: return ItemType.Null; } } }
The ideal quantity outcome when I add the item in (Item has a fixed quanityt of 1 for testing) is:
0 1 2 3 4 5 6 7 8 9... etc
But instead it goes like this:
0 1 2 2 2 2 2 2 2 2... etc
Where am I going wrong?
If more info is needed, I can give more.