Search Unity

EventSystem problem, OnDrag persisting after switching windows

Discussion in 'Scripting' started by Abrahamus, Dec 19, 2019.

  1. Abrahamus


    May 16, 2014
    Hi everyone,

    I've been making a game that uses drag and drop elements on a Canvas. The class responsible for that derives from:


    So I'm using the methods:

    OnBeginDrag(PointerEventData eventData), 
    OnBeginDrag(PointerEventData eventData),
    OnBeginDrag(PointerEventData eventData)

    I'm having problems while dragging an object, with this:

    Code (CSharp):
    1. public void OnDrag(PointerEventData eventData)
    2. {
    3.    transform.position = Input.mousePosition;
    4. }

    The problem is this one, if I drag the object, and keep doing it, and then switch tab, or simply lose focus of the game, then release the mouse and go back to the game, the object will keep following the cursor's position, despite the mouse not being clicked. I've tried different things so far, but nothing works, at some point I felt relief because it was a "known bug" that got fixed on Unityty 2018.2 (I was using 2018.1.9f2), then updated the project, but still having this issue.

    eventData.pointerDrag = null; 

    ^ Not working either, or disabling the GraphicRaycaster, the EventSystem, toggling the Run In Background option true, etc... Still would try anything at this point. Also need to try this again, but with an empty project, empty scene, you get the idea.

    Thanks for your time.

    EDIT 1: Tried with a new blank project using Unity 2018.4(LTS), if you use an Event Trigger with the Drag Event and begin dragging an object, and then you ALT TAB to lose and regain focus on the game, the object will keep dragging along the mouse position with no need to actually click.