Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by Tohron · Dec 11, 2014 at 05:29 PM · javascriptfunctioneventsystem4.6

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
21
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image f4bo · Nov 04, 2015 at 05:00 PM 2
Share

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");
     }
 }
 
avatar image
19

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image N3bul0 · Mar 10, 2016 at 11:57 AM 0
Share

I know it's an old question but your hint actually solved my problem... Thanks :)

avatar image RandomAgent · Apr 06, 2016 at 10:01 PM 0
Share

Can you elaborate on that and explain why this might help?

avatar image devonzhang11 · May 11, 2016 at 02:10 AM 1
Share

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.

avatar image slake_it · Aug 22, 2018 at 10:54 AM 0
Share

Thanks a lot

avatar image
4

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Create a new event for Button UI using script. 1 Answer

4.6 UI Full controller support 0 Answers

Performance Optimization ~Function Update: Loop or Once ? 5 Answers

4.6 UI Change what 'Spacebar' is doing by default 1 Answer

EventSystem - all touches have the same ID 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges