Inventory script help and how to remove items from inventory

Hi guys,

I found this inventory script on GitHub and for the most part, it’s been working fine and saving me from having to code a script myself: https://github.com/nzhul/inventory-system

There are some parts of the script I don’t understand, such as the else if statement in the below excerpt:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;

public class Slot : MonoBehaviour, IDropHandler
{
	public int id;
	private Inventory inv;
    public RectTransform invPanel;

    void Start()
	{
		inv = GameObject.Find("Inventory").GetComponent<Inventory>();

    }

    public void OnDrop(PointerEventData eventData)
	{
		ItemData droppedItem = eventData.pointerDrag.GetComponent<ItemData>();

        if (inv.items[id].Id == -1)
        {
            inv.items[droppedItem.slotId] = new Item();
            inv.items[id] = droppedItem.item;
            droppedItem.slotId = id;
            Debug.Log("Testing 1");
        }

        // ???
        else if (droppedItem.slotId != id)
        {
            Transform item = this.transform.GetChild(0);
            item.GetComponent<ItemData>().slotId = droppedItem.slotId;
            item.transform.SetParent(inv.slots[droppedItem.slotId].transform);
            item.transform.position = inv.slots[droppedItem.slotId].transform.position;

            droppedItem.slotId = id;
            droppedItem.transform.SetParent(this.transform);
            droppedItem.transform.position = this.transform.position;

            inv.items[droppedItem.slotId] = item.GetComponent<ItemData>().item;
            inv.items[id] = droppedItem.item;
            Debug.Log("Testing 2");
        }

	}
}

Apart from this, I have been trying to build a way to remove items from the inventory, which doesn’t seem to be part of the script. I tried adding the below snippet into the OnDrop() function:

else if(RectTransformUtility.RectangleContainsScreenPoint(invPanel, Input.mousePosition))
        {
            Debug.Log("Item dropped");
        }

And this into my Start() function:

        invPanel = GameObject.Find("InventoryPanel").GetComponent<RectTransform>() as RectTransform;

But the code doesn’t do anything. Any advice on what to do? Thank you!

Okay, that portion of code in else if() lets you change positions of one item with another item… but I still haven’t figured out how to drag/drop and remove an item from the inventory. Any help would be greatly appreciated. Thanks!

@TurboPipo, You can’t do it in the Slot class, by that time it is already too late - Because in the slot code you are dropping on a slot, it has no facility for knowing if you are dropping outside of a slot.

You have to implement the drag outside functionality at the higher level - probably you have a “container” class creates each slot, it is there you could check if you are dragging outside the bounds of the inventory object, or in some object you put in the background.

Where ever it goes, you put an OnDrop function and send a message (or call a method) to the item’s current Slot that the item is being removed, and likewise I would guess to the inventory manager system.