ontriggerenter start timer work but with a error.. :(

Hi i have this script…

private var startTime;
var textTime : GUIText;

function OnTriggerEnter (other : Collider){

startTime = Time.time;

}

function Update() {
line 12 error — var guiTime = Time.time - startTime;

var minutes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;

textTime.text = String.Format (“{0:00}:{1:00}:{2:00}”, minutes, seconds, fraction);
}

the script work but i have a error in line 12

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)
TimerScriptStart.Update () (at Assets\TimerScriptStart.js:12)

i dont understand really the error
my question is i want stop the timer when i enter in the other trigger and display on screen the time a with a option to save this time in the high score…:smile:

thank you for your help…

var startTime : float;

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

function OnTriggerEnter( other : Collider ) {
 var finalTime : float = Time.time - startTime;
 // do whatever
}

function Update() {
 var guiTime : float = Time.time - startTime;

 var minutes : int = guiTime / 60;
 var seconds : int = guiTime % 60;
 var fraction : int = (guiTime * 100) % 100;

 textTime.text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 
}

use (code) (/code) tags (with [ ]s instead of parentheses) to make people want to answer you :slight_smile:

NullReferenceException means you have a variable that is null but you are trying to use it (trying to use null.something or use null in a calculation). In this case you never set startTime before you started trying to access it.

Thank you vicenti for your help i trying now…

thank the script work good thank again…but the timer work anytime only reset to zero when touch the trigger