I have a GUI interface with a button for the character's attack, however I want to implement a defense button where the user can hold the button down to block incoming attacks, when the user stops touching the button then the player goes back to idle.

Right now I have the attack button working correctly, whenever the button gets pressed, it triggers a method where the animator gets called and performs the action. I want to implement the getButtonDown("") and getButtonUp("") used commonly with the keyboard input. How can i implement such fucntions to a GUI button?

  • \$\begingroup\$ could you explain how your currently implementing your attack button? There are differant ways, and a good answer would instruct in context of your current implementation. \$\endgroup\$
    – Gnemlock
    Sep 27, 2016 at 3:57

1 Answer 1


Attach the following script to your Block button GameObject. Or adapt into your current script.

The boolean 'isMouseDown' will be true while the mouse is held down. I haven't tried this code exactly, but it should work.

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class MouseEvents : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
    private bool isMouseDown = false;

    void Update() {
        // Is the mouse still being held down?
        if (isMouseDown) {
            // Keep blocking

    public void OnPointerDown(PointerEventData data) {
        isMouseDown = true;
        // Start blocking

    public void OnPointerUp(PointerEventData data) {
        isMouseDown = false;
        // Stop blocking


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .