Inventory stacking stops after first time

Hi, I’ve been making an inventory system and it seems to be working well other than the item stacking. I am able to place a stackable item in, then add one to that stack but after that it stops working.

Here is my inventory code, it is line 138 which is causing me problems, when the item stack in the inventory list is 2 it stops incrementing the item count. what confuses me is that it runs the code for that section (137 - 140) but just doesn’t seem to add to the inventoryList*.currentStack. I’m sure it will probably be something daft that I missed but I can’t for the life of me see it.*
```csharp
*using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]

public class InventoryManager : MonoBehaviour {

public List<Item> InventoryList = new List<Item>();
ItemDatabase itemDatabase;

public int inventorySize;
// Use this for initialization
void Start ()
{
    inventorySize = 24;

    itemDatabase = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemDatabase>();

    for (int i = 0; i < inventorySize; i++)
    {
        InventoryList.Add(new Item());  
    }

}

// Update is called once per frame
void Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        AddItem(1, 1);
    }
    if(Input.GetMouseButtonDown(1))
    {
        Debug.Log(itemDatabase.items[1].currentStack);
    }

    if(Input.GetKeyDown(KeyCode.A))
    {
        Debug.Log(InventoryList[0].itemName + " " + InventoryList[0].currentStack);
    }
    if (Input.GetKeyDown(KeyCode.S))
    {
        Debug.Log(InventoryList[1].itemName + " " + InventoryList[1].currentStack);
    }
    if (Input.GetKeyDown(KeyCode.D))
    {
        Debug.Log(InventoryList[2].itemName + " " + InventoryList[2].currentStack);
    }
    if (Input.GetKeyDown(KeyCode.F))
    {
        Debug.Log(InventoryList[3].itemName + " " + InventoryList[3].currentStack);
    }
}


void AddItem(int id, int amount)
{
   
    for (int i = 0; i < itemDatabase.items.Count; i++)
    {
        if(itemDatabase.items[i].itemID == id)
        {
            Item item = itemDatabase.items[i];
            if(item.maxStack != -1)
            {
                item.currentStack = amount;
            }
            AddItemAtCorrectSlot(item);
            break;
        }
    }
}


void AddItemAtCorrectSlot(Item newItem)
{
    //Check If stackable
    if(newItem.maxStack == -1) //Not Stackable
    {
        for(int i = 0; i < InventoryList.Count; i++)
        {
            if(InventoryList[i].itemName == null)
            {
                InventoryList[i] = newItem;
                break;
            }
        }
    }
    else if(newItem.maxStack != -1) //Stackable
    {
        bool isInInv = false;
        for(int i = 0; i < InventoryList.Count; i++)
        {
            if(InventoryList[i].itemID == newItem.itemID)
            {
                isInInv = true;
            }
        }
        if(isInInv == false) //Isn't in inventory
        {
            Debug.Log("NotInInv");
            for (int i = 0; i < InventoryList.Count; i++)
            {
               
                if (InventoryList[i].itemName == null)
                {
                   
                    InventoryList[i] = newItem;
                    break;
                }
            }
        }
        else //Is in inventory
        {
            Debug.Log("IsInInv");
            AddToStack(newItem);
            return;
        }
    }
}

void AddToStack(Item stackItem)
{
    for (int i = 0; i < InventoryList.Count; i++)
    {
        if (InventoryList[i].itemID == stackItem.itemID)
        {
            if (InventoryList[i].currentStack != InventoryList[i].maxStack)
            {

                int space = InventoryList[i].maxStack - InventoryList[i].currentStack;

                if (stackItem.currentStack <= space)
                {
                    Debug.Log("Current stack added " + stackItem.currentStack);
                    InventoryList[i].currentStack += stackItem.currentStack;
                    Debug.Log("Current Stack" + InventoryList[i].currentStack);
                    break;
                }
                else
                {
                    InventoryList[i].currentStack = InventoryList[i].maxStack;
                    stackItem.currentStack = stackItem.currentStack - space;
                    AddItem(stackItem.itemID, stackItem.currentStack);
                    break;
                }
            }

        }
    }
}

}*
```
Any help would be greatly appreciated.

Are you sure you want to break; there? That ends the loop. Maybe you mean continue; which goes to the next element in the loop, although you dont need it here at all.

Yeah, break is used when you want to break out of the loop. You have a lot of nested if’s, so it might be hard to notice, but your break will go all the way up to your for loop and break out of it.

Thanks for the replies guys, I took the breaks out of it but unfortunately I’m still having the same problem. Might have to see if I can work out a simpler method for these checks as it is pretty messy and try just rewriting it.

You have several other breaks throughout your code. Are you sure these are all intended breaks?

You should use some LinQ syntax, this is helpfull for clean code and for performance :

            Item targetItem = InventoryList.Where(oneItem => oneItem.itemID == stackItem.itemID).First();
            //First if you are sure the element is in the list, if not : exception
            //you can use FirstOrDefault, if the element is not in the list then return null

            if(targetItem.currentStack < targetItem.maxstack)
            {
                int space = targetItem.maxStack - targetItem.currentStack;
                ...
                ...

            }

Yeah, the other breaks in the AddItemAtCorrectSlot() prevent it from filling the inventory with the one item as it stops once the slot has been filled.

I haven’t come across this yet, I will take a look!

This current code creates the following problem: say your currentStack is at 4, and your max stack is 6. Your space will be at 2. So now, currentStack > space, and you will not add an item to your stack. Basically just do something like this:

```csharp

  •            if (InventoryList[i].currentStack < InventoryList[i].maxStack)
              {                   
                      Debug.Log("Current stack added " + stackItem.currentStack);
                      InventoryList[i].currentStack += stackItem.currentStack;
                      Debug.Log("Current Stack" + InventoryList[i].currentStack);
                      break;
              }*
    

```

I personally don’t like List.Where(linq).First
I’d rather do
if(List.FindIndex(linq) >= 0)
It effectively does the same thing, but gives you the index of the entry rather than the entry itself

Cheers for the advice guys, I used the list.FindIndex method to help cut down my code to a 50 line function that covers all the possibilities it needs. Fixed the problem and works beautifully now, as well as being easy to read!