I am trying to make the UI of my game’s inventory system. I currently am trying to trigger the OnDrop() function when the player’s cursor is over a panel. (The panel is invisible but takes almost the whole screen and serves as the area where the player can drop an item). Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ItemDropHandler : MonoBehaviour, IDropHandler
{
RectTransform invDropArea;
public bool isOverDroppableArea;
void Start()
{
invDropArea = GameObject.Find("DropArea").GetComponent<RectTransform>();
}
public void OnDrop(PointerEventData eventData)
{
if(isOverDroppableArea == true)
{
Debug.Log("Drop");
}
}
void Update()
{
isOverDroppableArea = RectTransformUtility.RectangleContainsScreenPoint(invDropArea, Input.mousePosition);
Debug.Log(isOverDroppableArea);
}
}
“isOverDroppableArea” becomes true using RectTransformUtility.RectangleContainsScreenPoint.
As soon as the cursor hovers over the area it stays true until it’s out of the area.
Even out of the if() statement no Debug.Log appears with OnDrop()
Here’s a quick video on what happens with the boolean:
Yes I did.
Debug.Log("invDropArea = " + invDropArea) returns the correct gameObject which has the RectTransform.
However, even modifying the Debug.Log(“Drop”) in the OnDrop() function to Debug.Log(isOverDroppableArea); doesn’t change much as the whole OnDrop() function never triggers.
Ah I found out why. I disabled completely the Image component from the panel, which needs to be enabled for the drop to work. Solved. Thanks for your help