I am making a 2D unity drag and drop quiz game.
The first thing I tried to make the OnDrop event work was to use the IDropHandler interface and apply that script to my answer slot. The code is below:
public void OnDrop(PointerEventData eventData)
{
Debug.Log("Ondrop");
}
However, in my console, I only received the OnEndDrag debug message but not the Ondrop. The solution I found was to togglecanvasGroup.blocksRaycasts
to true
after the EndDrag event so that the answer slot could detect the Ondrop event. However, this will not work in my situation because the draggable game object is not a UI element and does not have a canvasGroup associated with it. Is there a similar method to disable raycasts for my draggable game object, which is instantiated at runtime?
The second method I explored was using colliders. I had attached a non-kinematic rigid body to my incorrect answer slot for testing purposes and a box collider to both the draggable prefab and the incorrect answer slot. The code and inspector for the answer slot is shown below:
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("collision");
}
However, I did not get any debug message despite dragging the draggable over the answer slot.
The third method I tried was using triggers. For this, I had the same setup as the collider setup above, but checked the isTrigger boolean on the answer slot.
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("trigger");
}
The debug message was not logged to my console too.
What will be the best and easiest way for me to implement this OnDrop event for my quiz game, where the draggables are instantiated at runtime as non-UI elements? Thank you.