I can't take out an item from the slot of the inventory

I’m trying to create an inventory system for my vr unity project for university. The problem is that when I put an item in the slot everything works just fine but when i wanna take it out I can only change its position ma it remains attached to the slot. I’m going crazy, I even asked chatgpt and obviously it couldn’t help. Please explain me what I’m doing wrong

This is the script for the item:

'using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

public class Item : MonoBehaviour
{
public GameObject targetObject;
public Vector3 slotRotation;
public bool inSlot = false;
public Slot currentSlot;
private XRGrabInteractable grabInteractable;
private Rigidbody rb;

void Start()
{
    grabInteractable = GetComponent<XRGrabInteractable>();
    rb = GetComponent<Rigidbody>();

    if (grabInteractable != null)
    {
        grabInteractable.selectEntered.AddListener(OnSelectEntered);
    }
}

public void RemoveFromSlot()
{
    if (currentSlot != null)
    {
        currentSlot.RemoveItem(targetObject);
    }
}

public void OnSelectEntered(SelectEnterEventArgs args)
{
    if (inSlot)
    {
        RemoveFromSlot();
    }
}

}’

This is the script for the slot:

'using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.Interaction.Toolkit;

public class Slot : MonoBehaviour
{
public GameObject ItemInSlot;
public Image slotImage;
private Color originalColor;
void Start(){
slotImage = GetComponent();
originalColor = slotImage.color;
}

private void OnTriggerStay(Collider other) {
    if (ItemInSlot != null) return;
    GameObject obj = other.gameObject;
    if (!IsItem(obj)) return;

    XRGrabInteractable grabInteractable = obj.GetComponent<XRGrabInteractable>();
    if (grabInteractable != null && !grabInteractable.isSelected)
    {
        InsertItem(obj);
    }
}

private bool IsItem(GameObject obj) {
    return obj.GetComponent<Item>() != null;
}

private void OnItemGrabbed(SelectEnterEventArgs args, Collider other) {
    if (ItemInSlot == null) return;
    GameObject obj = other.gameObject;
    XRGrabInteractable grabbedItem = args.interactableObject as XRGrabInteractable;
    if (grabbedItem != null && grabbedItem.gameObject == ItemInSlot)
    {
        RemoveItem(obj);
    }
}

private void OnItemReleased(SelectExitEventArgs args, Collider other) {
    if (ItemInSlot == null) return;
    GameObject obj = other.gameObject;
    XRGrabInteractable releasedItem = args.interactableObject as XRGrabInteractable;
    if (releasedItem != null && releasedItem.gameObject == ItemInSlot)
    {
        // Detach item from the slot after release
        RemoveItem(obj);
    }
}

private void InsertItem(GameObject obj) {
    Rigidbody rb = obj.GetComponent<Rigidbody>();
    if (rb != null)
    {
        rb.isKinematic = true;  // Prevent physics interactions when inside the slot
    }

    obj.transform.SetParent(transform, true);  // Make item a child of the slot
    obj.transform.localPosition = Vector3.zero;
    obj.transform.localEulerAngles = obj.GetComponent<Item>().slotRotation;

    Item itemComponent = obj.GetComponent<Item>();
    itemComponent.inSlot = true;
    itemComponent.currentSlot = this;

    ItemInSlot = obj;
    slotImage.color = Color.gray;

    XRGrabInteractable grabInteractable = obj.GetComponent<XRGrabInteractable>();
    if (grabInteractable != null)
    {
        grabInteractable.selectEntered.AddListener(OnItemGrabbed);
        grabInteractable.selectExited.AddListener(OnItemReleased);
    }
}

public void RemoveItem(GameObject obj) {
    if (ItemInSlot == null) return;

    // Set parent to the root of the scene to detach from the slot
    obj.transform.parent=null;

    Item itemComponent = obj.GetComponent<Item>();
    if (itemComponent != null)
    {
        itemComponent.inSlot = false;
        itemComponent.currentSlot = null;
    }

    obj.GetComponent<Rigidbody>().isKinematic = false;

    XRGrabInteractable grabInteractable = obj.GetComponent<XRGrabInteractable>();
    if (grabInteractable != null)
    {
        grabInteractable.selectEntered.RemoveListener(OnItemGrabbed);
        grabInteractable.selectExited.RemoveListener(OnItemReleased);
    }

    // Reset slot and color
    obj = null;
    ResetColor();
}

public bool IsEmpty() {
    return ItemInSlot == null;
}

public void ResetColor() {
    slotImage.color = originalColor;
}

}’

Now they are both modified from gpt, by mistake I overwrite my old ones, but anyway the original where not so different from this

It just sounds like you wrote a bug… and that means… time to start debugging!

You might need to make some instrumentation to live-print what is happening as you interact with it, since it is XR and setting breakpoints can be extremely disruptive to the testing process.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

Not sure if this is what’s breaking it but I can notice that you’re not resetting ItemInSlot to null when removing an item.

Okay I’m gonna try this way

I was doing it but it wasn’t working either