Start timer with Key press

Hey,
I’m making a game with a timer. I want the timer to start when i press a key from the keyboard. Ive tried different things but nothing seems to be working :confused:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class timer : MonoBehaviour {
	public Text timerLabel;
	
	private float time;

	void Update() {
		time += UnityEngine.Time.deltaTime;
		
		var minutes = time / 60; //Divide the guiTime by sixty to get the minutes.
		var seconds = time % 60;//Use the euclidean division for the seconds.
		var fraction = (time * 100) % 100;
		
		//update the label value

		timerLabel.text = string.Format ("{0:00} : {1:00} : {2:000}", minutes, seconds, fraction);

		if (Input.GetKeyDown (KeyCode.T)) {

			//something 

		}

		if (Input.GetKeyDown(KeyCode.P))
		{
			if (Time.timeScale == 1)
			{
				Time.timeScale = 0;
			}
			else
			{
				Time.timeScale = 1;
			}
		}

		}

		}

Ok, I will add a modification of your script that should do what you want.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class timer : MonoBehaviour {
     public Text timerLabel;
     
     private float startTime = -1;
 
     void Update() {
         float time = startTime >= 0 ? UnityEngine.Time.time - startTime : 0;
         
         var minutes = time / 60; //Divide the guiTime by sixty to get the minutes.
         var seconds = time % 60;//Use the euclidean division for the seconds.
         var fraction = (time * 100) % 100;
         
         //update the label value
 
         timerLabel.text = string.Format ("{0:00} : {1:00} : {2:000}", minutes, seconds, fraction);
 
         if (Input.GetKeyDown (KeyCode.T)) {
 
             startTime = UnityEngine.Time.time;
 
         }
 
         if (Input.GetKeyDown(KeyCode.P))
         {
             if (Time.timeScale == 1)
             {
                 Time.timeScale = 0;
             }
             else
             {
                 Time.timeScale = 1;
             }
         }
 
         }
 
         }

This Should Work

//javascript
#pragma strict

private var TimerTime:float;
private var KeyPressTime;
private var TimerRunning:boolean = false;

function Update(){
    if(Input.GetKey("space")){
       TimerRunning = true;
       KeyPressTime = Time.time;
    }
    if(TimerRunning){
     TimerTime = Time.time - KeyPressTime;
   }
}