Search Unity

Function OnDrop() of EventSystems doesn't trigger

Discussion in 'Scripting' started by Gotthoms, Jan 17, 2020.

  1. Gotthoms

    Gotthoms

    Joined:
    Dec 30, 2019
    Posts:
    10
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class ItemDropHandler : MonoBehaviour, IDropHandler
    7. {
    8.  
    9.     RectTransform invDropArea;
    10.     public bool isOverDroppableArea;
    11.  
    12.     void Start()
    13.     {      
    14.        
    15.         invDropArea = GameObject.Find("DropArea").GetComponent<RectTransform>();
    16.  
    17.     }
    18.  
    19.     public void OnDrop(PointerEventData eventData)
    20.     {
    21.  
    22.         if(isOverDroppableArea == true)
    23.         {
    24.  
    25.             Debug.Log("Drop");
    26.  
    27.         }
    28.  
    29.     }
    30.  
    31.     void Update()
    32.     {
    33.  
    34.         isOverDroppableArea = RectTransformUtility.RectangleContainsScreenPoint(invDropArea, Input.mousePosition);
    35.         Debug.Log(isOverDroppableArea);
    36.  
    37.     }
    38.  
    39. }
     
  2. Deleted User

    Deleted User

    Guest

    I don't see where you set the "isOverDroppableArea" bool true. You should use
    Code (CSharp):
    1.             Debug.Log("invDropArea = " + invDropArea)
    in Start() to make sure that the object is found.

    Instead of
    Code (CSharp):
    1.             Debug.Log("Drop");
    use
    Code (CSharp):
    1.             Debug.Log(isOverDroppableArea);
    to make sure that it's true.
     
    Last edited by a moderator: Jan 17, 2020
  3. Gotthoms

    Gotthoms

    Joined:
    Dec 30, 2019
    Posts:
    10

    "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:
     
  4. Deleted User

    Deleted User

    Guest

    Maybe but did you use the Debug.Log() I mentioned to make sure of that?
     
  5. Gotthoms

    Gotthoms

    Joined:
    Dec 30, 2019
    Posts:
    10
    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.
     
  6. Deleted User

    Deleted User

    Guest

    Are yu sure nothing prevents the event to trigger on your UI elements?
     
  7. Gotthoms

    Gotthoms

    Joined:
    Dec 30, 2019
    Posts:
    10
    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