Timer not starting on GUI button click

G’day gang,
Trying to make an RPG style game, have written script for gui button and would like a timer to start once button is clicked but it only counts one for every click of the button. Was thinking that

if (GUI.Button (new Rect (170,300,300,20), “go do job”))
{
Timer ++;
}

would kick off a timer once clicked, but not.
any suggestions to get a timer cranking ?? Tried creating a new function so that the button click would go to the function to start a timer but only induced the same count.

cheers.

Timer++ will add 1 to the value in Timer. And naturally it will do this only when it’s called, so it does this once per click.

What you want is something like this:

float time;
bool isTimerOn;
void Update () {
  if (isTimerOn) {
    time += Time.deltaTime;
  }
}

and in your button code set isTimerOn = true;

Press button to start the timer-video

Below is my script to press the button to start the timer, i had zoom most of the part, so you can follow it well.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class timercountDown : MonoBehaviour {
float timer = 0;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	timer += Time.deltaTime;
}

void OnGUI()
{
	if (timer <10 ) {
		GUI.Label (new Rect (100, 100, 200, 100), "Timer =" + (int)timer);
	} else {
		GUI.Label (new Rect (100, 100, 200, 100), "Game over" );
	}
}

}