Reputation: 27
//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
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
Reputation: 2044
Put it OnGUI() such as
function OnGUI () {
GUI.Label (Rect (10,10,150,100), myTimer.ToString());
}
Upvotes: 3