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