- Home /
[4.6] GUI Button OnRelease?
Hey community,
in my current Android app made in Unity 4.6 I use the new Gui System. The new Button has its On Click() event which is working fine. But if the player is releasing the button the main charakter just walks endless further.
I tried to use the WaitForSecounds() methode to reset the button in a way, but it isnt working. This is my code so far:
using UnityEngine;
using System.Collections;
public class UIcontroller : MonoBehaviour
{
public bool pressedD = false;
public bool pressedU = false;
IEnumerator Wait(){
yield return new WaitForSeconds (3.0f);
}
public void ButtonDOWNpresser() //Called from the OnClick() section of the GUI button
{
pressed1 = true;
StartCoroutine(Wait());
pressed1 = false;
}
public void ButtonUPpresser()
{
pressedU = true;
StartCoroutine(Wait());
pressedU = false;
}
}
Is there no On Release() section to the On Click() section for the GUI button?
Thank you very much for your help!
Answer by Kiwasi · Nov 29, 2014 at 07:58 PM
Add an Event Trigger component to your button (In the event menu). From there you can add a listener for OnPointerUp. Treat it just the same as OnClick.
Note: It actually sounds like you want an OnPointerDown instead of OnClick. A click is defined as a button going down and then being released on the same element. So typically OnClick and OnPointerUp will be called in the same frame.
I see. Everything works fine now. Thank you very much
Been trying to figure out a way around having it only running my code on release! Thank you for saving me a few more hours of heartache! Event Trigger has saved my life!
why would there be such complex tutorials on the internet if it's that easy... thank you very much ! :)
I know this is an old question, but this method only works with $$anonymous$$ouse or Touch (in this case), if you're using Keyboard or Gamepad can you achieve the same "Down"/"Up" functionality?
Thanks!