- Home /
OnPointerClick function not triggering
I'm trying to write a script for a UI object that will detect when it is clicked on and be able to tell what kind of click it was (i.e. left click or right click). So far I just have a basic test script (written in Unityscript):
function Start () {
}
function Update () {
}
function OnPointerClick(data : UnityEngine.EventSystems.PointerEventData) {
print("Detection");
}
What I want it to do is print "Detection" whenever I click on it, but that isn't happening (eventually, I will look at the "data" variable to see what kind of click it was). What am I missing?
Answer by jenci1990 · Dec 11, 2014 at 06:42 PM
You need implements:
import UnityEngine.UI;
import UnityEngine.EventSystems;
public class TestEvents extends MonoBehaviour implements
IPointerClickHandler,
IPointerDownHandler,
IPointerUpHandler,
IPointerEnterHandler,
IPointerExitHandler,
ISelectHandler
/*...etc*/ {
function OnPointerEnter(eventData : PointerEventData) {
Debug.Log("Pointer Enter");
}
function OnPointerExit(eventData : PointerEventData) {
Debug.Log("Pointer Exit");
}
function OnPointerClick(eventData : PointerEventData) {
Debug.Log("Pointer Click");
}
function OnPointerUp(eventData : PointerEventData) {
Debug.Log("Pointer Up");
}
function OnPointerDown(eventData : PointerEventData) {
Debug.Log("Pointer Down");
}
function OnSelect(eventData : BaseEventData) {
Debug.Log("Select");
}
//...etc
}
Your class name and file name must b match! (I call it "TestEvents".)
since I found it useful as well here the c# flavour ready to go for those ho need it
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class event_catcher : $$anonymous$$onoBehaviour, IPointerClickHandler,
IPointerDownHandler,
IPointerUpHandler,
IPointerEnterHandler,
IPointerExitHandler,
ISelectHandler
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
public void OnPointerEnter (PointerEventData evd)
{
Debug.Log ("OnPointerEnter");
}
public void OnPointerExit (PointerEventData evd)
{
Debug.Log ("OnPointerExit");
}
public void OnPointerClick (PointerEventData evd)
{
Debug.Log ("OnPointerClick");
}
public void OnPointerDown (PointerEventData evd)
{
Debug.Log ("OnPointerDown");
}
public void OnPointerUp (PointerEventData evd)
{
Debug.Log ("OnPointerUp");
}
public void OnSelect (BaseEventData evd)
{
Debug.Log ("OnSelect");
}
}
Answer by turbanov · Sep 28, 2015 at 05:43 PM
For anyone coming to this question, you may also try adding a Physics Raycaster component to your active camera, and enabling an appropriate layer.
I know it's an old question but your hint actually solved my problem... Thanks :)
Can you elaborate on that and explain why this might help?
Thanks, your hint helps!
$$anonymous$$y case is a little different. I am trying to catch the pointer event with a normal GameObject ins$$anonymous$$d of UI object.
Here is some more details how it works for me : 1. add a collider component to the GameObject 2. implement interface IPointerDownHandler 3. add EventSystem to the scene 4. add Physics 2D Raycaster if you have a 2D collider, or Physics Raycaster for 3D collider.
thanks again.
Answer by Heremeus · Oct 09, 2016 at 06:59 PM
Another important hint: Check the EventSystem gameobject of the scene. The "Standalone Input Module" has to be enabled!
I disabled it to remove the "Submit" and "Cancel" input but then the mouse events are not triggered anymore.
Answer by Vodolazz · Nov 22, 2016 at 08:42 PM
Another one hint:
All ponter events will not trigger if clicked object is situated near the "far clipping plane" of the camera. There can be situation when object is seen in the scene but it doesn't trigger pointer events because is situated too near to the camera far border.
Was totally surprised by the fact and spent a lot of time solving this.
Answer by hally · Nov 29, 2019 at 02:00 AM
For me worked this:
Add Button component for your GameObject (in my case it was an Image with zero in alpha just to cover some area). Without it OnPointerClick() implemented from IPointerClickHandler was not triggered