- Home /
Run a function aslong as a button is pressed
Hi
I have a button in my canvas. I know how to make a button run a function whenever its pressed, but how do I make it run the function as long as I hold the button down.
To be more clear i'll give you an example :
I have a bool that needs to be turned on and off ( on when the button is pressed, and off if its not pressed)
e.g. :
public void WhileButtonIsPressed(){ myBool = true;
}
public void WhileButtonIsNotPressed(){ myBool = false;
}
long story short : I want my bool to be true if the button is held down, and false if its not.
Thanks in advance.
Have you given a thought on reading the Input documentation?
It is all there, Input.Get$$anonymous$$ey vs Input.Get$$anonymous$$eyUp
I know what you mean by getkey and getkey up , but unfortunately thats not the answer im looking for. See... if you press a button in an UI element it is a 1-frame action... but I need it to run the function attached to the button aslong as my mouse/ finger is pressing the UI button.
I think they're asking how do do that with uGUI, as in are there events besides OnClick... maybe.
Answer by sonu124 · Sep 17, 2015 at 12:14 PM
this code will work..
using UnityEngine; using System.Collections; using UnityEngine.EventSystems;
public class ButtonPressed : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
// Use this for initialization
void Start () {
}
void Update()
{
if (!ispressed)
return;
// DO SOMETHING HERE
Debug.Log ("Pressed");
}
bool ispressed = false;
public void OnPointerDown(PointerEventData eventData)
{
ispressed = true;
}
public void OnPointerUp(PointerEventData eventData)
{
ispressed = false;
}
}
Hi, Excuse me, I'm new to unity. How do I use this code? I can't seem to select the OnPointerDown function on the UI button's On Click () options.
HI there, welcome:
The GUI api has changed a lot in the last 6 years
https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UI.Selectable.OnPointerDown.html
The documentation says that you need to implement the IPointerDownHandler interface which will be part of what you need to do. Suffice to say you probably want to look at more modern solutions to your problem or ask a new question instead of digging up 6 year old+ posts.
Answer by Hiten2012 · Jul 04, 2015 at 01:43 PM
Its possible by many different ways but choose any suitable for your case. For UI button than you can refer this http://answers.unity3d.com/questions/857801/46-ui-dynamic-button-event-system-pointerenter-poi.html
If UI it not mandatory in your case You can also do it with GUI button than use Repeat Button http://docs.unity3d.com/ScriptReference/GUI.RepeatButton.html
In case of touch device input http://docs.unity3d.com/ScriptReference/TouchPhase.html
Answer by holliebuckets · Sep 16, 2015 at 10:30 PM
@Gilles_aerts This thread is a great resource too! ---> http://forum.unity3d.com/threads/touch-and-hold-a-button-on-new-ui.266065/
Your answer
![](https://koobas.hobune.stream/wayback/20220611225745im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
onMouseDown call function from other script 1 Answer
Boolean problem!! 2 Answers
Problems with save 1 Answer