[Help] Can't get the inventory stacking system right

I want to create something to stack my items, but I don’t know how. I want to have a maximum of how much it can stack and than use a new inventoryslot.
This is my code:


Scriptable Item code:

   using UnityEngine;
  
    [CreateAssetMenu(fileName = "new item", menuName = "Inventory/Item")]
    public class Item : ScriptableObject {
      
        new public string name = "new item";
        public Sprite icon = null;
        public GameObject objectToDrop;
        public string description;
        public int maxStackSize;
        public bool isStackable;
  
    }

The InventorySlot script:

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections.Generic;
    using System.Collections;
    using TMPro;
  
    public class InventorySlot : MonoBehaviour {

    public Image icon, icon2;
    public Button removeButton;
    public Transform player;
    public GameObject uiDescription, sellItem, nextStage;
    public TextMeshProUGUI textDescription, itemNameText;

    private Item item;

    private void Start()
    {
        icon.enabled = false;
        textDescription = textDescription.GetComponent<TextMeshProUGUI>();
        itemNameText = itemNameText.GetComponent<TextMeshProUGUI>();

    }

    private void Update()
    {
        if (icon.sprite != null)
        {
            icon.enabled = true;
        }
        else {
            icon.enabled = false;
        }
    }

    public void AddItem(Item newItem) {
        item = newItem;
        icon.sprite = item.icon;
        icon.enabled = true;
        removeButton.GetComponent<Image>().enabled = true;
    }

    public void ClearSlot() {
        icon.sprite = null;
        icon.enabled = false;
        removeButton.GetComponent<Image>().enabled = false;
    }

    public void OnRemoveButton() {
        PlayerInventory.instance.Remove(item);
        Instantiate(item.objectToDrop, player.transform.position, Quaternion.identity);
        uiDescription.SetActive(false);
        item = null;
    }

    public void UIOpenButton() {
        if (item != null) {
            uiDescription.SetActive(!uiDescription.activeSelf);
            icon2.sprite = item.icon;

            textDescription.text = item.description;
            itemNameText.text = item.name;
        }
    }
    }

The PlayerInventory script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
  
    public class PlayerInventory : MonoBehaviour {

    public GameObject inventory, escapeMenu, playerUIStats, playerSelflookInventory;
    public int inventorySpace;
    public List<Item> items = new List<Item>();
    public bool inventoryAcces;

    private bool inventoryClose;

    #region Singleton
    public static PlayerInventory instance;

    private void Awake()
    {
        if (instance != null) {
            Debug.LogWarning("More then one inventory");
            return;
        }
        instance = this;
    }

    #endregion

    public delegate void OnItemChanged();
    public OnItemChanged onItemChangedCallBack;

    private void Start()
    {
        inventoryAcces = true;
    }

    // Update is called once per frame
    void Update () {
        KeyInput();
        Inventory();
    }

    #region Inventory- &EscapeClose/Open

    public bool Add(Item item) {
        if (items.Count >= inventorySpace) {
            Debug.Log("not enough space to pickup");
            return false;
        }
        items.Add(item);

        if (onItemChangedCallBack != null)
        {
            onItemChangedCallBack.Invoke();
        }
        return true;
    }

    public void Remove(Item item) {
        items.Remove(item);
        if (onItemChangedCallBack != null)
        {
            onItemChangedCallBack.Invoke();
        }
    }
    }

(I have deleted some code in the PlayerInventory script about opening and closing the inventory, because I think it isn’t needed for this problem. If you need to see it please say it).


The inventoryUI updating script:

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

    public class InventoryUI : MonoBehaviour {

    public Transform itemsParent;

    private PlayerInventory inventory;
    private InventorySlot[] slots;

    void Start () {
        inventory = PlayerInventory.instance;
        inventory.onItemChangedCallBack += UpdateUI;

        slots = itemsParent.GetComponentsInChildren<InventorySlot>();
    }

    private void UpdateUI()
    {
        for (int i = 0; i < slots.Length; i++)
        {
            if (i < inventory.items.Count)
            {
                slots[i].AddItem(inventory.items[i]);
            }
            else
            {
                slots[i].ClearSlot();
            }
        }
    }
    }

If someone knows how to create the stacking system please let me know. I’m struggeling with it for almost a week now.


(I also have a bug with dropping a item and then pick it up, but when I pick it up it changed to a different item. I think I can do that by my own, but I have found the bug just a hour ago. It’s not my main question, but if you know what is causing that bug please let me know).


Thanks for helping.

Store the current stack size in the InventorySlot, and then define your behaviour for whatever happens when you add an object to that slot and it’s greater than the allowable stack size. Most games fill to the max capacity, and then your draggable item UI icon shows the new smaller stack size which you place in a different slot or discard. If you assume all items have a maximum stack size of 1 unless otherwise stated, you can co-opt the same logic for all items.

Yes I know the logic, but I can’t get it right, so if you do know how to achieve it and maybe write a bit of code to see your idee. Then I can create it to use in my own project.

I don’t know, something like this:

public void AddItem(Item newItem)
{
   if(item != null) // already something here
   {
       if(this.currentStackSize >= newItem.maxStackSize) // this will work for stack sizes of too:
       {
           // no room, we're replacing the previous items
           int previousStackSize = currentStackSize;
           Item previousItem = item; // hold onto the old item

           // set the info for this slot          
           currentStackSize = 1;
           AssignItemToThisSlot(item);

           // tell the UI to change the active item being placed
           // along with how many there are
           // because it needs to be moved to a new slot
           InventoryUI.SetActiveItemBeingMoved(previousItem, previousStackSize);
       }
       else
       {
           currentStackSize += 1;

           // update the UI with the new stack size?
       }
   }
   else
   {
       // nothing here, set the info for this slot:
       currentStackSize = 1;

       AssignItemToThisSlot(item); // set the icons and stuff
   }
}

Thanks I will try it, but I think I get your logic.
Thanks.