Many games involve players completing tasks or getting bonus points within a set time. This recipe shows how to create a basic countdown timer, similar to the one shown in the following screenshot:
To display a digital countdown timer, please follow this step:
// file: CountdownTimer.cs using UnityEngine; using System.Collections; public class CountdownTimer : MonoBehaviour { private float secondsLeft = 3f; private void OnGUI(){ if( secondsLeft > 0) GUILayout.Label("Countdown seconds remaining = " + (int)secondsLeft ); else GUILayout.Label("countdown has finished"); } private void Update(){ secondsLeft -= Time.deltaTime; } }
3.145.179.225