Removing wrong items from inventory

i’ve spent last 2 weeks trying to fix this weird bug, basically i have a Building, when i’m close to it and press “E” it’s UI will popup, i’ve made little script for copying Player Inventory and showing it inside that Building here is code for that:

public void PasteInventory ()
{
        ClearInventory();

        foreach( ItemSlot slot in Inventory.instance.InvUI.itemSlotList )
        {
            GameObject SlotGO = Instantiate( slotPrefab , InventoryCanvas );
            PlayerInventorySlot InvSlot = SlotGO.GetComponent<PlayerInventorySlot>();

            InvSlot.AddItem(slot.item);
            InvSlot.Count = slot.Count;
            InvSlot.CountText.text = slot.Count.ToString();

            StorageList.Add( InvSlot );
        }
    }

this works and all bla bla, then, i’ve added script for Inventory Slots that can be moved around and then moved to other Slots here is code for that:

using UnityEngine;
using UnityEngine.EventSystems;

public class PlayerInventorySlot : ItemSlot, IDragHandler, IBeginDragHandler, IEndDragHandler
{

    public bool isInSlot;
    public CanvasGroup canvasGroup;
    private Vector3 delta;

    public void OnBeginDrag ( PointerEventData eventData )
    {
        delta = transform.position - new Vector3(eventData.pressPosition.x, eventData.pressPosition.y, 0);

        canvasGroup.alpha = 0.5f;
        canvasGroup.blocksRaycasts = false;
        transform.SetParent( GameManager.CurrentUI.UI.transform );

        GameManager.CurrentUI.inv.StorageList.Remove( this );

        for( int i=0 ; i < Count ; i++ )
        {
            Inventory.instance.RemoveItem(item); //this is broken, more to that later
        }

        Inventory.instance.onItemChange.Invoke();

        if( GameManager.CurrentUI != null && GameManager.CurrentUI.inv != null )
        {
            GameManager.CurrentUI.inv.PasteInventory();
        }
    }

    public void OnDrag ( PointerEventData eventData )
    {
        transform.position = Input.mousePosition + delta;
    }

    public void OnEndDrag ( PointerEventData eventData )
    {
        canvasGroup.alpha = 1;
        canvasGroup.blocksRaycasts = true;

        if( !isInSlot )
        {
            for( int i=0 ; i<Count ; i++ )
            {
                Inventory.instance.AddItem(item); // this is things that is broken
            }

            if(GameManager.CurrentUI != null && GameManager.CurrentUI.inv != null)
            {
                GameManager.CurrentUI.inv.PasteInventory();
            }

            if( gameObject != null )
            {
                Destroy(gameObject);
            }
            //transform.SetParent(GameManager.CurrentUI.inv.InventoryCanvas.transform);
        }

    }

    public override void UseItem ()
    {
        //this is just to make sure Item won't be used
    }

}

PS: every slot have Count variable which is basically int that is set as TextField text
everything is working, except that line “Inventory.instance.RemoveItem(item)” when this is runned for some reason, Count variable in every other slots (except the one that is dragged) is set to 1, i tried doing everything, what i’ve found out is that for some reason function that actually removes Items from inventory is like broken or something and removes needed items and set every other slot to 1, i use this function in every single feature that needs to remove items from player Inventory, also here is code for that function:

public void RemoveItem ( Item item )
{
        foreach(ItemSlot slot in InvUI.itemSlotList.ToArray())
        {
            if(slot.item == item)
            {
                if( slot.Count > 1 )
                {
                    slot.Count--;
                    slot.CountText.text = slot.Count.ToString();
                }
                else if( slot.Count <= 1 )
                {
                    InventoryItemList.Remove(item);
                }
            }
        }
    }

i really don’t know what to do about this, i’ve spent 2 weeks trying to fix this, nothing works, sometimes it does and after while it magically breaks

if(slot.item == item)
One point of possible confusion I can immediately spot here is that you’re looking for items by it’s object memory reference and not by some kind of int or string GUID

string unique_item_guid = System.Guid.NewGuid().ToString();

This fact alone makes object comparison highly ambivalent, harder to debug (understand) and unnecessarily complicating serialization later on (i.e. game saves).
_
But that is not the main issue here. Your code doesn’t work because RemoveItem is completely broken. Why? Because you’re iterating it the wrong way: possibly multiple times, easily out or order, operating on a copy and iterating in wrong direction.

public void RemoveItem ( Item item )
{
    var list = InvUI.itemSlotList;
    for( int i=list.Count-1 ; i!=-1 ; i-- )
    {
        var slot = list*;*

if( slot.item.guid==item.guid )
{
slot.Count = Mathf.Max( slot.Count-1 , 0 );
slot.CountText.text = $“{slot.Count}”;
if( slot.Count==0 )
{
list.RemoveAt( i );
slot.Cleared();
}
return;
}
}
}
Removing multiple items from a list is a rare case where reversed iteration direction is helpful.