Search Unity

Inventory System: Item Adding Bug

Discussion in 'Scripting' started by Derpybunneh, Dec 28, 2016.

  1. Derpybunneh


    Mar 26, 2015

    (I've also posted this issue in the Unity Answers forum)

    So I'm working on a simple inventory script that will store items and tiles in a generic list. What it does is it finds the acquired tiles's ID, finds it in a database storing all the tile prefabs, and adds that prefab to the list with it's quantitative value. If a tile or item is acquired, and that item or tile already exists, it just adds to that tiles quantitative value.

    The following code I have seems to only work on the first tile I pick up - it will add more to it's quantitative value as I pick up more of the same tile. Let's call that tile A. But, if I acquire a different tile (tile B), and then go to pick up tile A, it will add to the list's A's quantitative value, but also create a new A.

    I don't know if there's something wrong with my loop or what, but it seems to not find the tile A that is in existence after I acquire a different tile and then go back to acquiring the tile A. I'm thinking of maybe instead of trying to look for that element in the list with the same game object tile I will have it look for a itemID in that element that reflects the ID I'm trying to either add to the list or add to the existent item's quantitative value.

    Hopefully that makes sense. If someone could help me fix my loop or something or show me what I've done wrong or could've done better on, I would appreciate that sooo much! Here's the code:

    Code (CSharp):
    1. public bool itemAlreadyExists;
    2. public int existingItem;
    3. public void addTile(int itemID) {
    4.      // check to see if tile already exists in list, then only add to its quantity
    5.      GameObject newTile = database.tiles [itemID];
    6.      for (int i = 0; i < items.Count; i++) {
    7.          if (items [i]._item == newTile) {
    8.              itemAlreadyExists = true;
    9.              existingItem = i;
    10.              print (itemAlreadyExists);
    11.              break;
    12.          } else {
    13.              itemAlreadyExists = false;
    14.              print (itemAlreadyExists);
    15.              break;
    16.          }
    17.      }
    18.      print (existingItem);
    19.      if (itemAlreadyExists == false) {
    20.          items.Add (new Item_Class {
    21.              _item = newTile,
    22.              _name =,
    23.              _description = newTile.GetComponent<BlockProp> ().description,
    24.              _quantity = newTile.GetComponent<BlockProp> ().quantity,
    25.              _type = 0
    26.          });
    27.      } else if (itemAlreadyExists == true) {
    28.          items [existingItem]._quantity += 1;
    29.      }
    30. }
    Last edited: Dec 28, 2016
  2. gorbit99


    Jul 14, 2015
    You should completely remove the else part of the thing, mostly because of the break part, because if the first item in the list isn't the item you're looking for, then it will say, that it isn't in the inventory, and create a new stack
    BTW: you can check booleans by simply doing if (isTrue) or if (!isFalse), where the first one works if isTrue is true, and the second one if isFalse is false
    You don't have to check itemAlreadyExists == true after the else statetement, because if isn't false, then it's true
  3. Derpybunneh


    Mar 26, 2015
    Thanks so much! That worked. I got rid of that else statement, as you recommended, and then in the else if statement I set itemAlreadyExists to false.