- Home /
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:
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?
Answer by Maxik2k · Jul 29, 2019 at 05:53 PM
Ok, I have just solved this problem! I had to attached Physics 2D Raycaster component on my Camera. Hope it will help someone else
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.
Answer by Nadir_Alishly · Sep 18, 2020 at 09:01 AM
There are three steps you need to check:
Object has Collider component on it. (use right collider, 3d not works for 2d and reverse)
Camera has Physics 2D Raycaster component.
Project has EventSystem object. (can be alone without canvas and etc.)
Had same problem, and those solved my problem.
Answer by Kennai · Jul 29, 2019 at 01:37 PM
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 IPointer$$anonymous$$oveHandler interface and added I$$anonymous$$oveHandler interface, but it still doesn't work:
public class Touch : $$anonymous$$onoBehaviour, IPointerDownHandler, IPointerUpHandler, I$$anonymous$$oveHandler
{
public bool is_Touched = false;
public void OnPointerDown(PointerEventData eventData)
{
is_Touched = true;
}
public void OnPointerUp(PointerEventData eventData)
{
is_Touched = false;
}
public void On$$anonymous$$ove(AxisEventData eventData)
{
Debug.Log("$$anonymous$$ove");
}
}
Answer by M-Hanssen · Jul 29, 2019 at 02:39 PM
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 StandaloneInput$$anonymous$$odule
Answer by BradyIrv · Jul 29, 2019 at 05:15 PM
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:
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Scripting button functionality inside class with created object's method 1 Answer
Distribute terrain in zones 3 Answers
Cannot change the value of this integer that I use for array,Cannot change the value of this one. 0 Answers
Add force when button is pressed 2 Answers