Making my items in my inventory stackable

Hello, I have a basic inventory system that is using scriptable objects to sort things into a simple ui slot system.
I am having trouble coming up with ways to make Items stackable so that it doesn’t constantly take up all the slot spaces, like most rpg games. Here are the scripts.

using UnityEngine;
using UnityEngine.UI;
public class Inventory2 : MonoBehaviour
{
    public Image[] itemImages = new Image[numItemSlots];
    public Gems[] items = new Gems[numItemSlots];
    public const int numItemSlots = 8;
    public void AddItem(Gems itemToAdd)
    {
        for (int i = 0; i < items.Length; i++)
        {
            if (items[i] == null)
            {
                items[i] = itemToAdd;
                itemImages[i].sprite = itemToAdd.gemImage;
                itemImages[i].enabled = true;
                return;
            }
        }
    }
    public void RemoveItem(Gems itemToRemove)
    {
        for (int i = 0; i < items.Length; i++)
        {
            if (items[i] == itemToRemove)
            {
                items[i] = null;
                itemImages[i].sprite = null;
                itemImages[i].enabled = false;
                return;
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


[CreateAssetMenu(menuName = "Mining/Gemstone")]
[System.Serializable]
public class Gems : ScriptableObject {
    public string gemstone = "";
    public int cost = 0;
    public string description = "";

    public string rarity = "Common";
    public bool usable = false;
    public Sprite gemImage;

    public bool stackable;
}

bump

An IStackable interface could add the functionality that you want.

If my thinking is right, all you really need is an int to keep track of how many of an item occupy that slot.

1 Like

My setup is like this:
Inventory script on the main panel, which handles searches, additions, removals
Slot script on each [slot]. This script has an item reference and a stack count variable.

My items had stackable, stacklimit (if stackable)
And I would only add to the stack if it was stackable, and i found the item before an empty slot and it’s stack count was less than the limit.

You can adapt it to your logic based on your needs/wants.

1 Like

Thank you for the replies, ill give your ideas a shot!

Do you have an example by chance?

Unity teaches better than I can.

1 Like

Thank you for the link! I’ve been working with Interfaces a bit and trying to use them for the needed purpose, and I have a very basic one using an int, however now, I’m trying to find a way to make it to where if an item is added to the list of slots i have, that it knows to stack it rather than make another image in another slot

You can check if an object implements an interface by using the “is” keyword.

That will tell you if your item is stackable or not. Then you can access the int where you keep the number of items stacked by casting the object to the interface type.