How would I add a count down timer that counts down per second and displays how many seconds left?
My Current Code: (yes I know that the Update function is incorrect but I'm just testing the decrementing)
var seconds = 60;
function Update () {
seconds --;
var a = GameObject.Find ("Timer");
var testMesh : TestMesh = a.GetComponent(TextMesh);
testMesh.text = seconds.ToString ();
When I used this code.... it went way too fast and went to negatives.
So can someone tell me a professional script (stops at zero and changes per SECOND)? Thanks!
My solution is in my “GameController” script, so the formatting (position, font, type, etc) are set in the inspector window. Also, the “GameOver” function I call, is inside the “GameController” script. Make sure you’ve got Using UnityEngine.UI listed at the top. I’ve also place the “(int)” in front of time remaining because I wanted it to countdown per second vs the timer showing per frame. Hope this helps!
public Text cdtimerText;
public float timeRemaining;
// Use this for initialization
void Start () {
cdtimerText.text = "";
}
void Update ()
{
// Functional game countdown timer
timeRemaining -= Time.deltaTime;
if (timeRemaining > 0) {
cdtimerText.text = "TIME: " + (int)timeRemaining;
} else {
cdtimerText.text = "TIME'S UP!";
GameOver ();
}
}