I am using EventSystems to register a click on a Game Object using the following:
using UnityEngine;
using UnityEngine.EventSystems;
public class ClickChecker : MonoBehaviour, IPointerClickHandler {
public void OnPointerClick (PointerEventData eventData)
{
Debug.Log ("clicked");
}
}
It should be noted that the methods in the IPointerEnterHandler and IPointerExitHandler are invoked, and I am getting the correct PointerEventData, but the method within the IPointerClickHandler interface, OnPointerClick, is not invoked.
(I have a Physics Raycaster attached to my camera, so this is not the culprit).
Added EventSystem game object to scene (Create → UI → Event System)
Camera has a Physics Raycaster (Select Main Camera, Add Component → Event → Physics Raycaster)
Selectable object is a MonoBehavior-derived class that implements IPointerClickHandler, IPointerDownHandler, and IPointerUpHandler (see accepted answer).
Selectable game object includes selectable object MonoBehavior script.
Selectable game object includes a collider (box, mesh, or any other collider type).
Check Raycaster Event Mask vs. game object’s Layer mask
Verify no collider (possibly without a mesh) is obscuring the selectable game object.
If collider is a trigger, verify that Queries Hit Triggers is enabled in Physics settings.
–
This list covers all the most common issues (Adding this list for posterity as this problem continues to trip me up every time I start a new project).
I have just run into this. By implementing IPointerUpHandler and IPointerDownHandler with empty methods, the OnPointerDown method was invoked as expected.
using UnityEngine;
using UnityEngine.EventSystems;
public class MyClickableBehaviour : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
{
public void OnPointerDown( PointerEventData eventData )
{
}
public void OnPointerUp( PointerEventData eventData )
{
}
public void OnPointerClick( PointerEventData eventData )
{
Debug.Log( "Clicked!" );
}
}
I had this issue as well what I had to do was add a Physics Raycaster script to my camera via the add component and searching "Physics Raycaster " every thing worked after that.
I was stuck on this as well. I had a Physics 2D Raycaster component attached to my main camera, and an EventSystem added to my scene.
My game object implemented the pointer handler interfaces, but what was missing for me was the (Box) Collider. Once I added that to my game object then pointer event handlers were working.
I did testing because I got it too:
Canvas
-Object1: IpointerDownHandler,IPointerUpHandler
–Object 2: IpointerClickHandler
Each ‘-’ represents parent, so object 2 is parented by object 1, which is parented by canvas.
Object1 steals the IPointerDownHandler and IPointerUpHandler events so Object 2 Cannot detect a click, since it uses those two events to do this.
I needed this particular hierarchy because object1 spawns lots of object2. Object 1 is a “draggable” window that needs to detect up and down events so I can stop my player from rotating when I drag a window. Detecting drag alone instead of the mouse events isn’t good enough, because it takes a few pixels of movement to be considered a drag, and my character turns 3 pixels every time.
Object 2 is a cancellable buff, which must live over top of the Object 1 buff bar and must respond to click events to cancel the buff on right click.
This seems like a bug to me since object2, being on top (or lower) in the hierarchy should intercept all events first IMO.