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.