SITUATION:
I have the following script on a UI GameObject A:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ScriptA: MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
Debug.Log("Drop");
}
}
On another UI GameObject B I have the following script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ScriptB : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
private Image image;
void Start()
{
image = GetComponent<Image>();
}
public void OnBeginDrag(PointerEventData eventData)
{
image.raycastTarget = false;
Debug.Log("Drag Start");
}
public void OnDrag(PointerEventData eventData)
{
Debug.Log("Dragging");
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("Drag End");
image.raycastTarget = true;
}
}
The hierarchy is (in parent → child order) UI Canvas → Container Image → GameObject A → GameObject B.
Both GameObjects have Image components that block raycasts by default. Container Image has a Grid Layout Group of several elements and GameObject A has a Grid Layout Group of one element.
There are no other GameObjects involved.
The expected behavior is that I depress the mouse button on GameObject B and “Drag Start” is printed to console once followed by a large quantity of “Dragging” while the object is being dragged, then when the object is brought over GameObject A and the mouse button is released “Drop” is printed once followed by “Drag End” once.
PROBLEM:
The actual behavior is different in that only 5-20% of the time is “Drop” printed.
Any ideas why, presumably, OnDrop isn’t being called every time? Everything else works 100%, it’s just this one last factor and I cannot troubleshoot. It seems entirely random whether or not it is called, though as I said the odds are on the low end.