Advertisement

Unity : OnPointerUp is firing at same time as isPointerDown on UI Button

Started by September 19, 2016 10:08 PM
2 comments, last by ferrous 8 years, 2 months ago

Hey guys and gals,

So I'm working in unity using C#. I am trying to get OnPointerUp / OnPointerDown to work. Right now I'm just using Debug.Log() to show that everything is working correctly. At first I had the Debug comands in the OnPointerUp / OnPointerDown methods and everything worked as expected. Then I moved the debug statements to the Update method and used an if/else statement to determine if the button is up or down. The weird thing is that the message to be shown OnPointerUp executes continuously even if I have the mouse pointer down. The message for OnPointerDown only goes when the mouse pointer is down.

I tried switching the else to an if else (I knew it would not work but i'm desperate) and I changed the Debug message to make sure that I don't have a random message in another code displaying the same thing.

Here's my code:


using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems; //Added

public class MultiTouchForMobile : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
    public GameObject Target;
    private bool isDown = false;//Is the button currently being held down
    private bool undo = false;//Keeps track of wether decrement has been called. If true decrement has not been called
    private float downTime;

    public void OnPointerDown(PointerEventData eventData) 
    {
        this.isDown = true;
        //this.downTime = Time.realtimeSinceStartup;
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        this.isDown = false;
    }
    void Update()
    {
        if(isDown)
        {
            Debug.Log("Down");
        }
        else
        {
            Debug.Log("UpEvent");
        }
    }
    public void Ignore()
    {
        Target.GetComponent<PlayerMovement>().AddIgnored();
    }
}

Thanks in advanced,

Zero

Looks right to me, but a thought I had was... is this script attached to multiple things?

Hello to all my stalkers.

Advertisement

(>.<) Wow, yah it was also attached to a empty Game. I forgot I originally had this script set up for OnClick and thus had it attached to a empty game object. Seriously I read your reply and went "I wouldn't do that" then decided to double check everything in the hierarchy and found it. Thank you For the help and I'm sorry that I wasted your time.

You can use parameters or log the game object directly. It can save a lot of time and hair pulling to do Debug.Log("OnPointerUp" + this.name);

Or Debug.Log("OnPointerUp", this.gameObject),

This topic is closed to new replies.

Advertisement