Search Unity

IPointerDownHandler on GameObject doesn't work ?

Discussion in 'Scripting' started by Korigoth, Mar 23, 2015.

  1. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    Hey,

    I was trying to implement User click with IPointerDownHandler on my GameObject
    so i tried this

    Code (CSharp):
    1. public class TilePoint : MonoBehaviour, IPointerDownHandler
    2. {
    3.         public void OnPointerDown(PointerEventData eventData)
    4.         {
    5.             Debug.Log("Down");
    6.         }
    7. }
    But the Debug is never showing in the unity console... so is it only for UI object?
     
  2. 2Mike

    2Mike

    Joined:
    Mar 23, 2015
    Posts:
    1
    Hello,
    I've got the same issue.
    Unfortunately, I don't know how to resolve it.
     
  3. D3ftp

    D3ftp

    Joined:
    Mar 11, 2015
    Posts:
    8
    I have the same problem ;/.
     
  4. Veris

    Veris

    Joined:
    Mar 2, 2015
    Posts:
    3
  5. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    my problem is that i doesn't have any collider too :p

    i will stay with my Engine that verify if Input.MouseButton(0) is fired
    and then check position to know which tile in my grid has been clicked!

    i wanted my tile to be able to react by theirself but it seems it cannot :S

    or does a collider enable = false will work?
     
  6. Ecocide

    Ecocide

    Joined:
    Aug 4, 2011
    Posts:
    293
    I don't think this works. But you could try setting IsTrigger to true. That way it won't collide with other objects but probably still work for OnMouseDown
     
  7. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    nice idea i will try it when i come back from work!
     
  8. D3ftp

    D3ftp

    Joined:
    Mar 11, 2015
    Posts:
    8


    I have did everything as in the tut but it still isn't working :<
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You need a bunch of things in place for the interfaces to work. Here is the list:
    • You need a GameObject in the scene with an EventSystem attached. This is created automatically when you create a Canvas, but you can also create one manually.
    • You need a PhysicsRaycaster attached to the appropriate camera.
    • You need a Collider attached to the GameObject
    • Your script implementing the interface must be attached to the GameObject.
    Do all of this and the EventSystem works like magic. Miss any of the steps and the whole thing won't work.

    I've been thinking of doing a video just about the EventSystem. Let me know if this would be useful. I don't think there is one in the learn section.
     
  10. D3ftp

    D3ftp

    Joined:
    Mar 11, 2015
    Posts:
    8
    i did everthing with your list.

    I added the following items
    • EventSystem
    • PhysicsRaycaster to my camera
    • GameObject with script and collider
    it still doesn't work ;/

    My Project Unity 5
    http://www.speedyshare.com/maFqn/CodeTest.rar -141KB
     
  11. Kiyote

    Kiyote

    Joined:
    Feb 22, 2013
    Posts:
    1
    Just so you know, you're not crazy. I decided to investigate the interfaces for handling input, and I get absolutely no response from any gameobjects implementing these interfaces.

    Obviously they must work for people, they wouldn't have shipped otherwise, but apparently there's a trick to using them that isn't obvious. It's not as simple as implementing this interface on a script and attaching that script to an object.

    Does the script have to descend from something else in particular? Can it be attached to arbitrary GameObject instances? Does it work on Instantiated GameObject instances? Do they have to be in the scene hierarchy right from the start?
     
  12. snertingen

    snertingen

    Joined:
    May 24, 2015
    Posts:
    1
    Is this still the way to do it in 2017 version of Unity? I have tried all this, but it don't work?
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yes, all of those steps are still the same to do it.
    This post is old. Please post a thread with your issue, showing your script and describing your issue. :)
     
  14. trungduc123

    trungduc123

    Joined:
    Jun 22, 2017
    Posts:
    2
    - Add EventSystem to scene.
    - Add Even Trigger to Gameobject
    - Add code to GameObject
    +
    void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
    {
    Debug.Log("OnPointerUp")
    }
    * Note: Your gameobject must have Box Collider
    - Add Physics Raycatser to Main Camera

    Good Luck!
     
    guneyozsan likes this.
  15. Crovea

    Crovea

    Joined:
    Feb 24, 2018
    Posts:
    1
    For people who were stuck with this problem like me in the future, the problem was that I had deleted my EventSystem gameobject that the canvas creates and which is needed.
    It should be a root game object with the components EventSystem and StandAloneInputModule

    Recreating that, fixed the problem for me
     
    TigerHix likes this.
  16. yetidevops

    yetidevops

    Joined:
    May 12, 2015
    Posts:
    2
    Another pro tip:
    1. Each canvas needs a GraphicsRaycaster. Including sub-canvases
     
  17. Damooon

    Damooon

    Joined:
    Apr 30, 2019
    Posts:
    1
    If you develop in 2D heres another "gotcha"

    I spent an hour trying to figure out why OnPointerDown would not fire on one of my gameobjects. Well turns out that gameobjects z-index was behind(?), outside of reach might be a better word, of my camera, Im guessing that causes no raycast hit.

    So make sure whatever gameobject you try to implement the handler interfaces on is in the cameras view (switching from 2D mode to 3D mode visualizes the problem)
     
    CringeItIn and Kiwasi like this.
  18. ahmmmmmwhy

    ahmmmmmwhy

    Joined:
    Aug 20, 2017
    Posts:
    11
    I had a problem with this. I had a blind spot where clicking did not lead to event, but outside of that area clicks went through.

    Turned out I had a blank TextMeshPro object for displaying errors — and it had RaycastTarget on. So it intercepted all clicks.

    Just had to turn RaycastTarget on any possibly blocking objects.

    Does not require:
    • Graphics raycaster, physics raycaster on camera
    • Collider

    Does require:
    • A dummy object with EventSystem attached
    • A script inheriting MonoBehaviour and implementing IPointerDownHandler
    • Raycast target on clickable image
    • No other objects covering it, or they have Raycast target disabled.
     
  19. CringeItIn

    CringeItIn

    Joined:
    Jan 24, 2021
    Posts:
    9
    Thank you man! Maybe a noob thing but your reply saved me a couple of more hours, I'm sure! Thanks a lot<3
     
  20. Chethan007

    Chethan007

    Joined:
    Dec 3, 2020
    Posts:
    57
    Works perfectly
     
    SillySillyGoosGoos likes this.
  21. thomasbarros_unity

    thomasbarros_unity

    Joined:
    Sep 9, 2022
    Posts:
    2
    I found the solution, you must put a physics raycaster component on the main cam, to work the raycast on a 3d object.
     
  22. thomasbarros_unity

    thomasbarros_unity

    Joined:
    Sep 9, 2022
    Posts:
    2

    I found the solution, you must put a physics raycaster component on the main cam, to work the raycast on a 3d object.