Unity3D – GetButtonDown – Timer

Create a scene with:

– GUIText

– EmptyObject and attach the script Timer.js:


#pragma strict

// Attach this script to a Empty GameObject in the scene

var currentTimeText : GUIText; // Assign a GUI Text in Inspector
var startTime = 0; // start time of the timer
var timeTaken : float; // Result sample -> 17.89256

function Start(){
} // -------------------------------------------------------------------- END Start()
 
function Update () {
// Take the time when you press Fire1 Button
if (Input.GetButtonDown ("Fire1")) { //Fire1 -> CTRL Left or Left Mouse Button
    Timer();     
}
	
} // -------------------------------------------------------------------- END Update()

function Timer(){
var timeTaken = startTime + Time.time; // refresh every 1/60 sec = 0,01666 sec                                      
currentTimeText.text = timeTaken.ToString (); // Result sample -> 17.89256
} // -------------------------------------------------------------------- END Timer()

      

Assign the GUIText variable in Inspector