Implementing IPointerClickHandler interface does not seem to work

Hello,

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).

IPointerClickHandler Not Working Checklist

  1. Added EventSystem game object to scene (Create → UI → Event System)
  2. Camera has a Physics Raycaster (Select Main Camera, Add Component → Event → Physics Raycaster)
  3. Selectable object is a MonoBehavior-derived class that implements IPointerClickHandler, IPointerDownHandler, and IPointerUpHandler (see accepted answer).
  4. Selectable game object includes selectable object MonoBehavior script.
  5. Selectable game object includes a collider (box, mesh, or any other collider type).
  6. Check Raycaster Event Mask vs. game object’s Layer mask
  7. Verify no collider (possibly without a mesh) is obscuring the selectable game object.
  8. 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).

2 Likes

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.

If your component is inside a canvas, another solution is to add a Graphic Raycaster component on the component to be clicked.

Add a collider to your game object.

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 have the answer to what causes this.

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.

A point that is worth mentioning:

You need to add the physics raycaster to the main camera that is compatible with the type of collider you are using in the target game object:

  • 3D colliders (without 2D suffix): add a PhysicsRaycaster
  • 2D colliders (with the suffix 2D): add a PhysicsRaycaster2D