Timer combined with OnPointerDown/Up doesnt count

Hello,

I am not a bit of a newbie Unity user, but i am learning as we speak.
Now, i have created the csharp below, added it to a button, but, it doesnt work…

It should start counting up when the button is being pushed down, and stop counting + populating “myText” with the number seconds pushed down, when released, with "timer : " in front of it.

The result wich i have right now is “timer : 0”, no matter how long i hold the button down.
I know i could achive this with add component → event trigger, but i prefer the C# method, mainly for future changes.

Does someone see what i did wrong and how i can make it work properly?

{
    float timer =0;
	public Text myText = null;
	int intTimer = 0;

	void Update () {
	}

	public void changetext()
	{
		intTimer = (int)timer;
		myText.text	= "timer : " + intTimer;
	}

	public void OnPointerUp(PointerEventData eventData)
	{
 		changetext ();
	}

	public void OnPointerDown(PointerEventData eventData)
	{
		timer += Time.deltaTime;
	}
}

{
float timer =0;
public Text myText = null;
int intTimer = 0;
bool counting = false;

     void Update () {
if(counting) timer += Time.deltaTime;
     }
 
     public void changetext()
     {
         intTimer = (int)timer;
         myText.text    = "timer : " + intTimer;
     }
 
     public void OnPointerUp(PointerEventData eventData)
     {
counting=false;
          changetext ();
     }
 
     public void OnPointerDown(PointerEventData eventData)
     {
         counting =  true;
     }
 }

edit your code like this.
and you are good to go.