Stopping Timer when hitting a Collider

Hello, ive been trying to go at this for hours. so far i have a timer which starts counting as soon as i hit play. when the controller hits the collider for some reason or another the timer keeps on counting. Any ideas how i can fix this please?

var time : float;
var elapsedTime : float;
var curTime : float;
 
function Update(){
	time = Time.time;
	elapsedTime = time - Time.time;
}
 
function OnGUI(){
	GUI.Box (Rect (10,10,400,60), "Elapsed Time:" + time);

}

function OnTriggerEnter(hit : Collider){
	if(hit.gameObject.tag == "Finish"){
	// this i where im stuck
}
}

3 Answers

3

This is my solution.

var time : float;
var elapsedTime : float;
var curTime : float;
var isStopped : bool = false;
function Update(){

    if (!isStopped){
        time = Time.time;
        elapsedTime = time - Time.time;
    }
}
 
function OnGUI(){
    GUI.Box (Rect (10,10,400,60), "Elapsed Time:" + time);
 
}
 
function OnTriggerEnter(hit : Collider){
    if(hit.gameObject.tag == "Finish"){
    // this i where im stuck
        isStopped = true;
    }
}

Well, one way would be to put the timer code inside an if-statement which only executes if a flag is set to true. Then, in your OnTriggerEnter(), you set the flag to false once the Finish collider has entered.

Something like:

var time : float;
var elapsedTime : float;
var curTime : float;

var myFlag : boolean;

function Start() {

     myFlag = true;
}
 
function Update(){

    if (myFlag){
        time = Time.time;
        elapsedTime = time - Time.time;
    }
}
 
function OnGUI(){
    GUI.Box (Rect (10,10,400,60), "Elapsed Time:" + time);
 
}
 
function OnTriggerEnter(hit : Collider){
    if(hit.gameObject.tag == "Finish"){

    // this i where im stuck

    myFlag = false;
}

Thanks Guys worked fine… was having problems with collider & player tags :slight_smile:

So is one of those answer right ?

–