- Home /
OnBeginDrag, OnEndDrag not being called for unknown reasons
Hello there. So I'm fairly new to Unity, and have been following tutorials and the such. I'm now in the process of making my first game. Since I'm new, I've been adapting things from tutorials and example projects to build my game.
I've decided to reproduce a classic card game. For this card game, I'd like to drag cards, which would follow the mouse pointer around, until dropped in certain zones. I'd like to use the IDragHandler to make a script which I can attach to UI elements on a canvas to have their position adjusted to the position of the mouse when it is being dragged.
To do this, I need to detect when an object is dragged. Baby steps first, using the following code I should get a console log when I begin and stop dragging objects :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[RequireComponent(typeof(Image))]
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
void Start()
{
Debug.Log("GameStart");
}
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("OnBeginDrag");
}
public void OnDrag(PointerEventData eventData)
{
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("OnEndDrag");
}
}
I attached this script to a UI image, parented to a panel, parented to a Canvas with an EventSystem in the scene. I also added a Canvas Group component to the image and have interactable and blocks raycasting enabled. The Camera also has a 2-d Physics Raycaster attached. (I'm sure some of this is unnecessary, but I tried them anyhow)
When I run the scene and attempt to drag and drop my image, I expect the console to have three logs - GameStart, OnBeginDrag and OnEndDrag. However, all I have is GameStart.
I've compared to the Unity Samples : UI store asset and I also checked out a tutorial video by quill18 creates at this youtube link. I should be getting console debug logs, but I'm not and am at a loss as to what to check for next.
Any help would be greatly appreciated.
Answer by Hunted223 · Dec 31, 2017 at 12:42 AM
Thanks to some knowledgeable friends in Twitch Chat, the answer was found and I figured I would share here.
It seems that some time between 2015 and now, an update was released that changed the default setting on UI images. "Raycast Target" on the "Image ( Script)" is now disabled by default and needs to be enabled for the DragHandlers to function properly.
Koodo's to kdo_92 and his twitch chat on Twitch.
I still have the problem... :/ the project worked fine on Unity 5 but since i updated to 2019 i can't OnDrag my Joystick on any mobile.. only works in editor. Help?
Perhaps you should start your own question? What you're asking isn't clear. There's no mention of a joystick anywhere in this question
I tried but also got an error, I'll try again now/later
Your answer
Follow this Question
Related Questions
Drag and Drop lagging behind mouse 0 Answers
Possible to drag items across tabs onto the same frame? 1 Answer
How to transition a click-and-drag from a UI button to a GameObject 0 Answers
How to drag Unity Objects to other c# application 0 Answers
Drag and drop files from the assets folder to another folder not working 0 Answers