How can i make my Timer script start and pause only when something certain is activated?

Hiya guise. I have a script here that works like a timer. And essentially i want it to only record when my NightVision is enabled (when you press N button), and when you disable nightvision the timer pauses, to start from where it was when you enable nightvision again etc. How can i go about doing this? Suggestions would be much appreciated!

CODE:

#pragma strict

private var startTime : float;
var textTime : String;

function Start ()
{	
	startTime = Time.time;
}



function OnGUI ()
{
	
		var guiTime = Time.time - startTime;
		
		var minutes : int = guiTime / 60;
		var seconds : int = guiTime % 60;
		var fraction : int = (guiTime * 100) % 100;
		
		textTime = String.Format ("REC {0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
		GetComponent(GUIText).text = textTime;
		GetComponent (GUIText).color = Color.red;
		GetComponent (GUIText).pixelOffset = Vector2 (-480, 250);
		GetComponent (GUIText).fontSize = 25;
}

you dont want to know startTime - current time
what you want is to increase the timer during the Update() or onGUI() method
JS isnt my strong suit but i hope i get my point across

function Start(){
    timer = 0;
}

function onGUI () {
    if(nightVisionIsActive){
        timer += Time.deltaTime; //<--this is the amount of time since last frame
    }
}

you need to keep track of when you are in night vision, so just toggle the bool in onKeyDown() then the timer will only count up when it is active

Instead of " var guiTime = Time.time - startTime;", try adding Time.deltaTime.

deltaTime is time since last frame.

Also, I don’t really know JS but the logic is sound. I’m more used to c#.

var time = 0;
var timerOn = true; //this should be set to true or false when N is pressed

function OnGUI()
{
if(timerOn == true)
time += Time.deltaTime;
}