- Home /
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).
Does your object with this script on have an appropriate 3D collider? You also need an EventSystem object in your scene.
Yes, it's just a Box with a Box Collider. And an Event System is in place.
Update: recreated from a scratch project, the method is now invoked. Not sure what's the cause, will update again if / when I know.
I can't say this is a true answer, only a guess.
Its possible that everything is set up correctly but the click event is getting passed to a different object (whether that object is listening for that event or not). a good way to test what your actually clicking on is to select the Eventsystem and expand its info on the inspector (you need to drag up the bar thats usually on the bottom of the Inspector panel). while in-game when you click on something the EventSystem will populate this info and tell you what the pointer is over at the time.
Answer by roddles · Sep 19, 2017 at 05:30 AM
IPointerClickHandler Not Working Checklist
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
, andIPointerUpHandler
(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).
There is also a new point on the list, to be checked.
If you upgrade to the new Input System, you need to update the EventSystem GameObject. There is a button in the Inspector of that component, to swap to the new "Input System UI $$anonymous$$odule Script Component".
Answer by adamonline45 · Nov 19, 2015 at 11:00 PM
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 too had this issue, and your solution solved it.
987456149865496856468531685340 / 10 => would definitely recommend.
Is this because the OnPointerClick event needs a corresponding "Down" and "Up" event before a "Click" is registered?
I also wonder why OnPointerUp() and OnPointerDown() have to be implemented (even if empty)... shouldn't OnPointerClick() work anyway?
Answer by Fimgers · Oct 22, 2016 at 06:27 PM
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.
This still works in Unity 2020.2.15. Note that you don't need the OnPointerUp or OnPointerDown methods with this answer.
Answer by obstinate · Aug 14, 2017 at 10:08 AM
If your component is inside a canvas, another solution is to add a Graphic Raycaster component on the component to be clicked.
Answer by WigglingPotatoDev · Sep 14, 2017 at 06:31 AM
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.
Your answer

Follow this Question
Related Questions
Use Event Triggers or something similar on non-UI objects? 0 Answers
Change sprite on click 0 Answers
Image appear on click 0 Answers