Problems with Drag Handlers Unity 5.6

I recently found a weird problem with my Drag handlers where when I drag my object it goes in the “OnBeginDrag” just fine but never gets to “OnDrag” or “OnEndDrag” (figured this out using Debug.Log), I don’t really understand why and also this problem only occurs on Unity 5.6 (I made the same code work on Unity 5.5) does anyone know what could be causing my problem?

public void OnBeginDrag(PointerEventData eventData)
    {
        LayoutElement le = placeholder.AddComponent<LayoutElement>();
        le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
        le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
        le.flexibleWidth = 0;
        le.flexibleHeight = 0;

        placeholder.transform.SetSiblingIndex( placeholder.transform.parent.childCount );
      
        parentToReturnTo = this.transform.parent;
        placeholderParent = parentToReturnTo;
        this.transform.SetParent( placeholder.transform );
      
        GetComponent<CanvasGroup>().blocksRaycasts = false;

       //Does everything here just fine
    }
  
 public void OnDrag(PointerEventData eventData) {
      //Never enters here
        this.transform.position = eventData.position;
    }
  
    public void OnEndDrag(PointerEventData eventData) {
      //Never enters here
        this.transform.SetParent( parentToReturnTo );
        this.transform.SetSiblingIndex( placeholder.transform.GetSiblingIndex() );
        GetComponent<CanvasGroup>().blocksRaycasts = true;
        // Set it back to a 0,0,0 position in order to snap to the old location
        GetComponent<RectTransform>().anchoredPosition3D = Vector3.zero;
        // Remove the placeholder element
        Destroy(placeholder);
    }

Use Code Tags!

and comment out the BlocksRaycast line.

the Eventsystem still needs to be able to raycast the target. it only makes event calls on the best object it hits with its RaycastAll (finds all objects it hit, filters to only those that have the specific interface, and then chooses the closest one).

Don’t worry, your OnDrop event handlers should get called even if the dragged object is over the drop area. Since the event system would hit it in that same raycastAll, but filter to IDropHandler instead

Even when I comment out the BlocksRaycast line it still doesn’t work, it never enters any other handler than OnBeginDrag, even OnDrop doesn’t work.

What interfaces have you defined in the class header? are you certain on drag is never getting called?

I ask because eventdata.position is in screenspace and your setting it to position which is world space.
for 2d/3d objects you typically want to use.

        if(eventData.pressEventCamera)
            transform.position =eventData.pressEventCamera.ScreenToWorldPoint(eventData.position);

while canvas/UI objects need something like RectTransformUtility:

        Vector3 point =  transform.position;
        if(RectTransformUtility.ScreenPointToWorldPointInRectangle(transform as RectTransform, eventData.position, eventData.pressEventCamera, out point))
            transform.position = point;

there could be a number of reasons why your dragging isn’t working (or it is working just imperceptible). but theres not enough information provided to point it out for you.

  • Is the eventsystem active and enabled in the scene?

  • Is the object enabled in the scene? (matters for some events but not most)

  • For 2d/3d objects:

  • Do you have a the appropriate Physics2DRaycaster/PhysicsRaycaster on the primary camera?

  • Can that raycaster cast against the layer your object is on?

  • Does the camera cull the layer your object is on?

  • Does your object have a collider component?

  • For Canvas/UI objects:

  • Do you have a GraphicsRaycaster on a canvas parent?

  • Does your raycast cast against the layer your object is on?

  • Is your object parented somewhere in the canvas?

  • Does your object have an image component?

  • Does your component have a canvas group that blocks raycasts disabled?

  • Does your script implement IBeginDragHandler, IDragHandler, and IEndDragHandler?

I should clarify that OnDrag and OnEndDrag are specific events that should fire even if its not raycasted by the pointer. so the blocksRaycast shouldn’t matter here

But that’s the thing, it never goes in those handler at all and I know this because I put some Debug.Log(“Begin”);, Debug.Log(“On”); and Debug.Log(“End”); at the beginning of each handler and my console only ever shows me “Begin” never the 2 others.

like I said before, you haven’t provided enough information to help you. You haven’t posted the class definition nor answered any my questions.

if OnDrag is not getting called then theres a reason, I’ve listed a number of possible reasons because you haven’t given me anything else to go on. until you do so you’re gonna have to figure it out on your own.

1 Like
  • Yes

  • Yes

  • Canvas/UI Objects :

  • Yes

  • Yes

  • Yes

  • Yes

  • Yes

  • Yes

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;

public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
  
    public Transform parentToReturnTo = null;
    public Transform placeholderParent = null;
    private ItemController itemController;

    GameObject placeholder = null;

    void Start()
    {
        itemController = this.GetComponent<ItemController>();
    }


    public void OnBeginDrag(PointerEventData eventData)
    {
if (placeholder == null)
        {
            placeholder = new GameObject();
            // Set it in front of ALL of the UI elements
            placeholder.transform.SetParent(UIService.Instance.MiniInventoryCanvasTransform);
            placeholder.transform.name = "CardHolder";
        }
        LayoutElement le = placeholder.AddComponent<LayoutElement>();
        le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
        le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
        le.flexibleWidth = 0;
        le.flexibleHeight = 0;

        placeholder.transform.SetSiblingIndex( placeholder.transform.parent.childCount );
      
        parentToReturnTo = this.transform.parent;
        placeholderParent = parentToReturnTo;
        this.transform.SetParent( placeholder.transform );
      
        GetComponent<CanvasGroup>().blocksRaycasts = false;

    }
  
    public void OnDrag(PointerEventData eventData) {

        this.transform.position = eventData.position;
    }
  
    public void OnEndDrag(PointerEventData eventData) {

        this.transform.SetParent( parentToReturnTo );
        this.transform.SetSiblingIndex( placeholder.transform.GetSiblingIndex() );
        GetComponent<CanvasGroup>().blocksRaycasts = true;
        // Set it back to a 0,0,0 position in order to snap to the old location
        GetComponent<RectTransform>().anchoredPosition3D = Vector3.zero;
        // Remove the placeholder element
        Destroy(placeholder);

        EventService.Instance.QueueEvent(new OnCardPlacementDone(null));
    }

}

I don’t have much more to say. I looked into the code and the PointerInputModule will always call OnDrag on the same frame and same gameobject as OnBeginDrag. Only time it wont is if the components on that gameobject, with the interface, are disabled during the OnBeginDrag.

only thing left I can say is make a bug report, preferably with the project package included, or steps to reproduce it in an empty project.