user3323352
user3323352

Reputation: 27

How to display the Timer Countdown on the Screen

//A simple countdown timer

var myTimer : float = 5.0;

function Update () {
 if(myTimer > 0){
  myTimer -= Time.deltaTime;
 }
 if(myTimer <= 0){
  Debug.Log("GAME OVER");
 }
}

This is a countdowntimer script I just want it to show it up on the screen while it counts down .

Upvotes: 1

Views: 6286

Answers (2)

toreau
toreau

Reputation: 660

You probably also want to have this outside the Update() method, and have the logic run only when it's necessary. This can be done by using the InvokeRepeating method like this (C#):

float myTimer = 5.0f;

void Start() {
    InvokeRepeating( "DecreaseTime", 1, 1 ); // Called every second
}

void DecreaseTime() {
    myTimer--;
}

void onGUI() {
    GUI.Label(new Rect(10,10,400,90), "myTimer = " + myTimer );
}

Upvotes: 2

jparimaa
jparimaa

Reputation: 2044

Put it OnGUI() such as

function OnGUI () {
    GUI.Label (Rect (10,10,150,100), myTimer.ToString());
}

Upvotes: 3

Related Questions