OnPointerClick not firing, what am i missing?

@Laperen : It’s weird you can’t get that to work. I find that interface very useful. :slight_smile:

Thanks for the reply. I Googled for a few solution but not sure what you would call it so not much luck. Got results about Unitys new input manager but that is not really the same thing i am after as far as i understand. I checked out the hololens project but it was a bit complicated and i decided not to waste much more time on it. So for now i have gone with firing a graphics and physics ray cast every frame and handling the results myself.

There is nothing wrong about it. :slight_smile: As I said, your idea is basically the same, with the only difference that you’ve currently limited it to the mouse so it might end up being a little more specific.
You can always extend that and you’re right, sometimes you just don’t need such complexity - it’s often rather SDK/Framework development that tries to cover a large variety of potential use cases.

Anyway, just to encourage you to follow your idea: The more complex input systems just do it pretty much the same way, there’s a single entity somewhere burried in the system that fires one raycast every frame, which then updates all kind of stuff such as things like ‘pointer entered on gameobject XYZ, pointer exited … , hovering over GameObject’ and throws the corresponding events.

As complex as it may sound, it’s actually quite nice.
Often you find many scripts in a project that do all fire their own raycast, extract the object that was hit and so on and so forth - even if the input information is always the same, such as center of screen, no specific layer … In large projects that can cause quite some significant overhead.

From what I’ve understood you want to avoid that redundancy, and that’s a good thing.
Keep it up!

2 Likes

You are very helpful. Great to get some reassurance i am not doing something bad or reinventing the wheel too much!

I don’t understand.
I have a simple (test) 2D scene.
I have 2 UI panel in it (auto creating canvas and eventSystem).
In one of the Panels, I create an Empty GameObject.
On this empty gameObject I add a UI Button as a child object.
Then I add a Physic Raycaster 2D on the main camera
Then I add a 2D box collider on the empty gameObject (well not empty anymore)
I then move the handle of the 2D box collider to fullfill the GameObject
Then i create this C# script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class SimpleLiteEventHandler : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
{  

    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("LITE OnPointerClick");
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("LITE OnPointerDown");
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        Debug.Log("LITE OnPointerUp");
    }
}

I add this script to the “empty” GameObject.

I hit play and nothing happen when i click on space around the Button (corresponding to the area of the 2d box collider) or on the button itself !

It only work when i remove the script from “empty” gameobject and attach it to the child UI button istead !

But I want the script to work on the “empty” gameobject level !

I realize that you are new here, but in the future if you have a question, please post a thread of your own and describe the problem/question, as you did well, mind you :slight_smile:

Your issue is that there is no raycast target, I believe. If you add an image it will work. You can set the image’s alpha to zero to mimic it not being there. :slight_smile:

Thanks for the answer.

… I though it would be more logic not to create a new thread of the same question …

Adding a transparent image does the trick, but then I don’t even need a collider ! So i suppose theses are for the game object in scene out of UI canvas ?

The new issue is that it work on the space around the UI button but not on the button itself as it take over priority to handle clicks.

But I want both :
When I clicks on the button I want
The normal behavior of the button clicked to fire (for now only a tint change as default)

  • My script behavior (Debug.Log())

Hmm, that’s odd, because I tried a setup like so:
panel
child empty / image
child of empty/ image : button

and I can interact with the button without any issue.

And yes, generally speaking I would say the 2d raycaster is for sprites with a collider, 3d for other game objects with colliders, and just the graphics raycaster for UI elements, with a raycast target component.

Yes you can interact with the button like me, but it doesn’t log anymore “Debug.Log(“LITE OnPointerClick”);” and the 2 other

This is because the button has also an Image component with a Raycast target in it. So either I let this checked and i can interact with the button but don’t have the log anymore on console, or I uncheck it and I have the log on console but can’t interact with the button anymore.

If I want both I don’t know how to (easily) do it.

Ah okay, I misunderstood a bit…so why not just put the script on both game objects?

You need an EventSystem in your Hierarchy, otherwise the events won’t be triggered.

4 Likes

I’ve had the same problem, however for a canvas UI object, all I did was add a button component to the object and suddenly the OnPointerClick works. This was very elusive because both OnPointerEnter and OnPointerExit were working, but not the Click.

I had this issue and I was missing the interface extension in the class declaration like this

public class ItemHandler : MonoBehaviour, IPointerDownHandler
{

I know this thread is old, but hopefully this helps someone!

Just in case someone lands here with my issue, I had created an image with Event Trigger / PointerDown and it was driving me nuts that it wouldn’t work. Turns out the Image was not on layer UI. It was on layer Default. UPDATE: Actually no lol it was the Override Sorting checkbox that got left on and set to 0 on the Canvas in DoozyUI. For some reason visually my button was showing up but was unclickable. Turning off Override Sorting fixed it. UPDATE2: Landed here again because I had a Canvas Group on the object with block raycast unchecked.

Thanks guys I was missing the EventSystem in the scene for gameobjects, all works for me too now.

1 Like

I had a similar problem. In my case, an empty Event Trigger component has been added to the game object I want to click.
When I removed the Event Trigger component, OnPointerClick started firing in code.

Solved!
There was a transparent Canvas covering the entire screen with “Block Raycast” checkbox active.
I simply disable it and IpointerClick worked.

1 Like

You are my hero!!! Don’t be afraid of answer any question even if it is old.

I have an issue where OnPointerClick is firing from a mouse click but not from a gamepad press. My gamepad input works fine otherwise. Does this funciton not work with gamepads? If so what are the alternatives?

it depends on how you are accessing your gamepad input. the event system has what are called InputModule components on the EventSystem gameobject. By default they support touch input and standalone (eg keyboard+mouse). on those modules they have a BaseInput class which handles the hardware-level gathering of input data.

you can extend/override this system at runtime by implementing a class that derives from UnityEngine.EventSystem.BaseInput and setting that class instance to the inputmodule’s inputOverride property when you want it to override the default implementation. In that class you can specify if there should be a mouse cursor present and where it should be on that frame based on gamepad input. You can then set inputOverride back to null whenever and the inputmodule will revert to the normal input behavior.