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:
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 cursorOnDrop()
gets called when dropping Rect on Rect(1) or vice versaOnDrop()
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;
}
}
Answer by BusyRoots · Jan 19, 2018 at 06:50 PM
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).
Your answer
Follow this Question
Related Questions
IDropHandler only trigger when it's SpriteRenderer sortingOder is higher than draggable sortingOder 0 Answers
transform.postion behaving strangely when dragging an object 0 Answers
How to render an image always on top? Already tried some solutions 0 Answers
How to Event tigger when drag and drop complete 1 Answer
IPointerExitHandler while holding click 0 Answers