[Answered](C#) Stacking Items in a Inventory

Hello Unity Community,

What I am trying to do is if an item can be stacked then stack it. But I am running into a problem. I have a currentStack amount on the Item itself, when I add an item this number goes up-Not the problem- , The problem is when the item gets to its maxStack amount and I try to add just one more item, It adds a new stack of 5(maxStack).

Please help me with this, I have been trying to find a solution for a while now, but with no success.

Here is my code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class Item
{
	public string itemName;
	public int itemID;
	public bool itemStack;
	public int itemCurrentStack;
	public int itemMaxStack;

	public Item(string name, int id, bool stack, int maxStack)
	{
		itemName = name;
		itemID = id;
		itemStack = stack;
		itemCurrentStack = 1;
		itemMaxStack = maxStack;
	}

	public Item()
	{
		itemID = -1;
	}
}

public class ItemDataBase : MonoBehaviour
{
	public List<Item> items = new List<Item>();

	void Start()
	{
		items.Add(new Item("Test Item", 0, true, 5));
	}
}

And my Inventory Script

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Inventory : MonoBehaviour
{
	public int slotsX = 5, slotsY = 5;
	public List<Item> inventory = new List<Item>();

	//Get Scripts
	private ItemDataBase dataBase;

	void Start()
	{
		//Set slots to Empty
		for(int s = 0; s < slotsX * slotsY; s++)
		{
			inventory.Add(new Item());
		}

		//Get Scripts
		dataBase = GameObject.FindGameObjectWithTag("Game Manager").GetComponent<ItemDataBase>();

		//Add Items For Testing
		AddItem(0);
	}

	void Update()
	{
		if(Input.GetKeyDown(KeyCode.Space))
			AddItem(0);

		if(Input.GetKeyDown(KeyCode.R))
			RemoveItem(0);
	}

	void AddItem(int id)//With stacking the currentStack count is being added to all of the items of the same id, and more items still fill the inv.
	{
		for(int i = 0; i < inventory.Count; i++)
		{
			if(inventory<em>.itemStack && inventory<em>.itemID == id && inventory_.itemCurrentStack < inventory*.itemMaxStack)//Stacking*_</em></em>

* {*
_ inventory*.itemCurrentStack ++;//This makes the actual item have a higher current stack*
* break;
}
else if(inventory.itemName == null)
{
for(int d = 0; d < dataBase.items.Count; d++)
{
if(dataBase.items[d].itemID == id)
{
inventory = dataBase.items[d];
break;
}
}
break;
}
}
}*_

* void RemoveItem(int id)*
* {*
* for(int i = 0; i < inventory.Count; i++)*
* {*
if(inventory_.itemID == id && inventory*.itemCurrentStack > 1)
{
inventory.itemCurrentStack --;
break;
}
else if(inventory.itemID == id)
{
inventory = new Item();
break;
}
}
}
}*_

I am going to answer my own question, just in case anyone needs to know the answer in the future.

What I did my not be the best way to do it but it works for what I need.
What I did was make a new public class and put the currentStack amount in there and then put the Item in there too.

Here is the code for that

[System.Serializable]
public class StackItem
{
	public Item item = new Item();
	public int amount = 1;
}

If you really want to stack items then look at the generic Stack class in System.Collections.Generic. This way you can properly implement the Last In First Out behaviour. If you don’t want that you can probably look at other generic Collections like Queues.