- Home /
The question is answered, right answer was accepted
GameObject blocking OnPointerEnter
Hello! I have a problem with my drag and drop inventory system. My problem is that the item i am dragging is on top of the slot like it should be. But. Because of that. The slot's "OnPointerEnter" Isn't being called because my mouse is on the item. Not the slot. Is there a way to fix this?
Here is my DragDetection Script for the slots:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class slotDragDetection : MonoBehaviour {
public int slot;
bool pointerEntered = false;
bool pointerClicked = false;
void Awake() {
EventTrigger trigger = gameObject.AddComponent<EventTrigger> ();
EventTrigger.Entry entry = new EventTrigger.Entry ();
EventTrigger.Entry entry2 = new EventTrigger.Entry ();
EventTrigger.Entry entry3 = new EventTrigger.Entry ();
EventTrigger.Entry entry4 = new EventTrigger.Entry ();
//Entry one
entry.eventID = EventTriggerType.PointerEnter;
entry.callback.AddListener( ( data ) => { onPointerEnter( (PointerEventData)data ); } );
trigger.triggers.Add( entry );
//Entry two
entry2.eventID = EventTriggerType.PointerExit;
entry2.callback.AddListener( ( data ) => { onPointerExit( (PointerEventData)data ); } );
trigger.triggers.Add (entry2);
//Entry three
entry3.eventID = EventTriggerType.PointerDown;
entry3.callback.AddListener( ( data ) => { onPointerDown( (PointerEventData)data ); } );
trigger.triggers.Add (entry3);
//Entry four
entry4.eventID = EventTriggerType.PointerUp;
entry3.callback.AddListener( ( data ) => { onPointerUp( (PointerEventData)data ); } );
trigger.triggers.Add (entry4);
}
public void onPointerEnter(PointerEventData data) {
pointerEntered = true;
Debug.Log ("Entered");
}
public void onPointerExit(PointerEventData data) {
pointerEntered = false;
Debug.Log ("Exited");
}
public void onPointerDown(PointerEventData data) {
pointerClicked = true;
}
public void onPointerUp(PointerEventData data) {
pointerClicked = false;
}
}
And here is my item dragging script so i can drag items:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class dragItems : MonoBehaviour {
Transform defaultSlot;
float offsetX;
float offsetY;
void Awake() {
EventTrigger trigger = gameObject.AddComponent<EventTrigger> ();
EventTrigger.Entry entry = new EventTrigger.Entry ();
EventTrigger.Entry entry2 = new EventTrigger.Entry ();
EventTrigger.Entry entry3 = new EventTrigger.Entry ();
//Entry one
entry.eventID = EventTriggerType.BeginDrag;
entry.callback.AddListener( ( data ) => { onBeginDrag( (PointerEventData)data ); } );
trigger.triggers.Add( entry );
//Entry two
entry2.eventID = EventTriggerType.Drag;
entry2.callback.AddListener( ( data ) => { onDrag( (PointerEventData)data ); } );
trigger.triggers.Add (entry2);
//Entry three
entry3.eventID = EventTriggerType.EndDrag;
entry3.callback.AddListener( ( data ) => { onEndDrag( (PointerEventData)data ); } );
trigger.triggers.Add (entry3);
}
public void onBeginDrag( PointerEventData data) {
offsetX = transform.position.x - Input.mousePosition.x;
offsetY = transform.position.y - Input.mousePosition.y;
if (transform.parent.parent.parent != null && transform.parent.parent.parent.CompareTag ("inventory")) {
defaultSlot = transform.parent.transform;
transform.SetParent (transform.parent.parent.parent);
}
}
public void onDrag(PointerEventData data) {
transform.position = new Vector3 (offsetX + Input.mousePosition.x, offsetY + Input.mousePosition.y);
}
public void onEndDrag(PointerEventData data) {
transform.position = defaultSlot.transform.position;
transform.SetParent (defaultSlot);
}
}
Edit: Is there a way for a raycast to ignore a layer? This would help me out if it's possible. I cannot just simply disable raycast target on the items because that would make the item not dragable anymore
Answer by FortisVenaliter · Mar 21, 2017 at 09:59 PM
You're using the UGUI system, right?
Usually, what I do in these situations is disable the checkmark for Raycast Target on the graphics of the dragged item. That way the events and all will ignore it while it's being dragged. If you're not instantiating that for dragging, be sure to re-enable them after it's dropped though.