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.

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;
}

}

Its possible by many different ways but choose any suitable for your case.
For UI button than you can refer this 4.6 UI Dynamic button event system PointerEnter, PointerExit, PointerUp etc. - Questions & Answers - Unity Discussions

If UI it not mandatory in your case
You can also do it with GUI button than use Repeat Button Unity - Scripting API: GUI.RepeatButton

In case of touch device input Unity - Scripting API: TouchPhase

@Gilles_aerts This thread is a great resource too! —>
http://forum.unity3d.com/threads/touch-and-hold-a-button-on-new-ui.266065/