OnPointerDown and OnPointerUp doesn't work

Hey! A have a strange issue - OnPointerDown and OnPointerUp events doesn’t work without any errors in the Editor. Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;   
    public class Touch : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
        public bool is_Touched = false;
    
        public void OnPointerDown(PointerEventData eventData)
        {
            is_Touched = true;
            Debug.Log("Dowm");
        }
    
        public void OnPointerUp(PointerEventData eventData)
        {
            is_Touched = false;
            Debug.Log("Up");
        }
    }

and here is my game object with this script:

alt text

When I use OnMouseDown and OnMouseUp it works! But I don’t have to use this methods, because they don’t work correctly on my android device. What’s wrong with my OnPointerDown and OnPointerUp script?

6 Answers

6

Ok, I have just solved this problem! I had to attached Physics 2D Raycaster component on my Camera. Hope it will help someone else

It did help. Thank you.

worked for me. thx.

You're my hero, sir!

You also need a 2D collider on the thing you attach pointer down handler to. It didn't work for me when I used a 3D mesh collider, for instance.

There are three steps you need to check:

  1. Object has Collider component on it. (use right collider, 3d not works for 2d and reverse)
  2. Camera has Physics 2D Raycaster component.
  3. Project has EventSystem object. (can be alone without canvas and etc.)

Had same problem, and those solved my problem.

1 Like

Are you sure you have the following things in place?:

  • Collider on the monobehaviour (if it’s not a UI object)

  • EventSystem (with StandaloneInputModule)

Yes, I'm sure. This is UI object and it has Box Collider 2D and EventSystem has StandaloneInputModule

OnPointerDown works like OnTriggerEnter system.

Create a canvas, under this one create an image that takes up the whole screen (click on rect image on the left-top corner, use option for mac or ctrl for windows).

So know you have Canvas, with an image full screen. Put your script with OnPointerDown handler or anything else.

Click on your screen :slight_smile:

Definately! Otherwise it will call OnPointerUp when you move the knob

Hello, @GG_WP_EASY !

I noticed one strange bug with this Pointer interfaces. if your class dosnt implement IPointerMoveHandler, then IPointerUpHandler is called when you first time move pressed finger.

Add IPointerMoveHandler interface with dummy method and check again if it fixed your problem on Android.

I didn't find IPointerMoveHandler interface and added IMoveHandler interface, but it still doesn't work: public class Touch : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IMoveHandler { public bool is_Touched = false; public void OnPointerDown(PointerEventData eventData) { is_Touched = true; } public void OnPointerUp(PointerEventData eventData) { is_Touched = false; } public void OnMove(AxisEventData eventData) { Debug.Log("Move"); } }

Does the object which has this script attached to it have the “Event Trigger” component?

No, it had hasn't. I attached "Event Trigger" component, added new events PoinerDown and PointerUp, drag my gameobject, chose Touch function (script is "Touch.cs"), but there aren't my OnPointerDown and OnPointerUp methods: ![alt text][1] [1]: /storage/temp/143704-1.png