Variable gets overwritten when changing from OnDrag() to OnDrop()

Hi everyone,

I’am using the unity event system for my drag & drop functionality in my inventory. I experienced a change of the variable value between the “drag calls”: OnBeginDrag(), OnDrag(), OnEndDrag() and the “drop call” OnDrop().
I created an example to show the problem:

  • created a scrpit ‘TestDragDrop’
  • script is attached to two identical gameobjects Rect and Rect(1) which are placed in the scene, see:
    109526-rects-in-scene.jpg

Inspector of Rect(1) (identical to Rect):

TestDragDrop(Script) - working principle:

  • In Start() I declare _testNumber = 3

  • OnBeginDrag() sets _testNumber = 5

  • during OnDrag() dragged gameobject (Rect or Rect(1) follows mouse cursor

  • OnDrop() gets called when dropping Rect on Rect(1) or vice versa

  • OnDrop() output says _testNumber = 3 ← Why??? (i want it to be: _testNumber = 5)

  • output of OnEndDrag() says _testNumber = 5 ← okay

I hope somebody can help me. If I forgot some information feel free to ask me :).

Here the complete script:

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

using UnityEngine.UI;

public class TestDragDrop : MonoBehaviour, IBeginDragHandler, IDragHandler, IDropHandler, IEndDragHandler
{

    private int _testNumber;

    // Use this for initialization
    void Start()
    {
        Debug.Log("Start");
        _testNumber = 3;
    }

    
    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("OnBeginDrag");
        _testNumber = 5;
        Debug.Log("_testNumber = " + _testNumber);

        GetComponent<CanvasGroup>().blocksRaycasts = false;
        GetComponent<Image>().raycastTarget = false;
    }


    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("OnDrag");
        Debug.Log("_testNumber = " + _testNumber);

        // mouse Coordinates need to be converted to World coordinates
        var mouseCoordinates = Input.mousePosition;
        // use the inverted z-coordinate to make sure object gets displayed 
        // in front of the camera
        mouseCoordinates.z = -GameObject.Find("MainCamera").transform.position.z;
        transform.position = Camera.main.ScreenToWorldPoint(mouseCoordinates);
    }


    public void OnDrop(PointerEventData eventData)
    {
        Debug.Log("OnDrop");
        Debug.Log("_testNumber = " + _testNumber);
    }


    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("OnDragEnd");
        Debug.Log("_testNumber = " + _testNumber);

        GetComponent<CanvasGroup>().blocksRaycasts = true;
        GetComponent<Image>().raycastTarget = true;
    }


}

I figured it out. Actually everything is working how it should be. The mistake sat in front of the computer :D.
Since the OnDrop() function gets called when you drop something an the game object (where OnDrop() is attached to) it gives me as output _testNumber = 3 because OnBeginDrag() hasn’t been called on this game object.
For a visual explanation see the attached image from my console output where I dropped the game object Rect on the game object Rect(1).

109616-console-output-with-gameobject-names.jpg