# Stopping Timer when hitting a Collider

**URL:** https://discussions.unity.com/t/stopping-timer-when-hitting-a-collider/62502
**Category:** Questions & Answers
**Created:** [February 6, 2013, 1:22am UTC](https://discussions.unity.com/t/stopping-timer-when-hitting-a-collider/62502 "2013-02-06T01:22:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Enrique13](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@Enrique13](https://discussions.unity.com/u/Enrique13)
#### Post date: [February 6, 2013, 1:22am UTC](https://discussions.unity.com/t/stopping-timer-when-hitting-a-collider/62502/1 "2013-02-06T01:22:46Z")

</div>

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
}
}

```

---

<div class="post-metadata">

### Author: ![pakizip](https://avatars.discourse-cdn.com/v4/letter/p/b782af/32.png) [@pakizip](https://discussions.unity.com/u/pakizip)
#### Post date: [February 6, 2013, 3:20am UTC](https://discussions.unity.com/t/stopping-timer-when-hitting-a-collider/62502/2 "2013-02-06T03:20:45Z")

</div>

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;
    }
}

```

---

<div class="post-metadata">

### Author: ![BilboStabbins](https://avatars.discourse-cdn.com/v4/letter/b/f08c70/32.png) [@BilboStabbins](https://discussions.unity.com/u/BilboStabbins)
#### Post date: [February 6, 2013, 3:06am UTC](https://discussions.unity.com/t/stopping-timer-when-hitting-a-collider/62502/3 "2013-02-06T03:06:55Z")

</div>

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;
}

```

---

<div class="post-metadata">

### Author: ![Enrique13](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@Enrique13](https://discussions.unity.com/u/Enrique13)
#### Post date: [February 6, 2013, 7:57am UTC](https://discussions.unity.com/t/stopping-timer-when-hitting-a-collider/62502/4 "2013-02-06T07:57:45Z")

</div>

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