Why OnDrop is not called? (Unity)

Viewed 743

I have had problems with the OnDrop method, it is that it is not called, I was reading and maybe it has something to do with a Raycaster component, but I'm not sure, I don't even have knowledge of it, if someone could explain this to me I would greatly appreciate it.

Here is my code in c # plus an image of my hierarchy in Unity2D:

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

public class Drop : MonoBehaviour, IDropHandler
{
    public void OnDrop(PointerEventData eventData)
    {
        if (eventData.pointerDrag != null) 
        {
            Debug.Log("Aqui esta");
        }
    }
}

enter image description here

From already thank you very much.

1 Answers

OnDrop method is not called

To ensure the Method gets called you need to ensure that both the name and inherited class you use is correct.

As it seems you need to use override for each function and instead of IDropHandler and MonoBehaviour inherit from EventTrigger.

Example:

using UnityEngine;
using UnityEngine.EventSystems;

public class Drop : EventTrigger {
    public override void OnDrop(PointerEventData eventData) {
        // Check if the eventData is Null.
        if (eventData.pointerDrag != null) {
            Debug.Log("OnDrop called.");
        }
    }
}

EventTrigger

Related