Timer and gui text

hi guys this is my simple timer and is working perfect, but how i can display the countdown of the timer in screen with a gui text i get a error when i try and i must do something with string…

var timer : float = 10;

function Update(){

timer -= Time.deltaTime;

}

so we have the variable timer and is float and equal with 10
and timer get subtracted with Time.deltaTime every frame so that it but how i can display the minutes/seconds as gui text?

You must create a GUIText and set a reference to it in your script, which will become something like this:

var gText: GUIText; // drag here the GUIText from Hierarchy view
var timer: float = 10;

function Update(){
  timer -= Time.deltaTime;
  if (timer < 0) timer = 0; // clamp the timer to zero
  var seconds: int = timer % 60; // calculate the seconds
  var minutes: int = timer / 60; // calculate the minutes
  gText.text = minutes + ":" + seconds;
}

This formatting doesn’t work fine with negative numbers, thus the timer is clamped to zero. If you need to display negative values, change the function Update to this:

...
function Update(){
  timer -= Time.deltaTime;
  var t = Mathf.Abs(timer); // get the absolute timer value
  var seconds: int = t % 60; // calculate the seconds
  var minutes: int = t / 60; // calculate the minutes
  var minSec = minutes + ":" + seconds; // create the formatted string
  if (timer < 0) minSec = "-" + minSec; // add a minus sign if negative
  gText.text = minSec; // update the GUIText
}

EDITED:

String concatenation isn’t a good idea because it generates lots of intermediate memory allocations, which may cause performance hiccups when garbage collection (free memory recuperation) takes place. A better solution is to use .NET/Mono String.Format:

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

Each number is defined in the formatting string inside curly brackets, where the digit before the colon tells which argument to use (starting at 0), and the digits after the colon show the format. Take a look at .NET docs for more details.

This topic has been discussed extensively. A search engine is your friend …

http://answers.unity3d.com/questions/11458/How-to-make-a-timer-in-the-game.html