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).
2 Likes
Does your object with this script on have an appropriate 3D collider? You also need an EventSystem object in your scene.
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.
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).
2 Likes
There is also a new point on the list, to be checked. 9. 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 Module Script Component".
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 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.
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.
Does your object with this script on have an appropriate 3D collider? You also need an EventSystem object in your scene.
– KiwasiI 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.
– JoshuaMcKenzie