- Home /
OnDrop never gets called
Hey guys, I'm trying to make a simple point & click game, and basically what i'm trying to do is drag and drop a UI image (a key) from my inventory to a regular gameObject (a door). I'm using the drag and drop interfaces but the OnDrop method of the door gameObject never gets called, any help?
This is the script that I use for my inventory items such as the key: Everything works fine here. I read a similar issue in another thread where they said you have to use a canvasgroup, which I did but that also didn't help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ItemDragHandler : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler {
Vector3 startPos;
public GameObject storedItem;
CanvasGroup group;
#region IBeginDragHandler implementation
public void OnBeginDrag (PointerEventData eventData)
{
print("begin drag");
startPos = GetComponent<RectTransform>().localPosition;
group.blocksRaycasts = false;
}
#endregion
#region IDragHandler implementation
public void OnDrag (PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
#endregion
#region IEndDragHandler implementation
public void OnEndDrag (PointerEventData eventData)
{
print("end drag");
GetComponent<RectTransform>().localPosition = startPos;
group.blocksRaycasts = true;
}
#endregion
// Use this for initialization
void Start () {
group = gameObject.GetComponent<CanvasGroup>();
}
}
and this is the script I use for the door
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Door : UsableObject, IDropHandler {
bool unlocked;
[SerializeField] Sprite spriteClosedDoor;
[SerializeField] Sprite spriteOpenedDoor;
#region IDropHandler implementation
public void OnDrop (PointerEventData eventData)
{
print("dropped!");
}
#endregion
}
Here are the inspectors of the two gameObjects:
The key:
The door:
Thanks a lot.
Answer by flynt_1995 · Jan 21, 2019 at 08:39 PM
And here is the hierarchy
Your answer
Follow this Question
Related Questions
How to move UI image between panels 0 Answers
Cannot drag-and-drop ui.text in inspector 1 Answer
Grid Inventory : Move Item & Slot Highlighting 2 Answers
Drag And Drop Window 1 Answer