Count down from 100 to 0 in x sec.

Hey there,

I am looking for a way to count down from 100 to 0 in lets say 2 seconds. How do I do that ?

I know how to create a normal countdown using Time.deltaTime but not sure how to approach a countdown in a predefined timeframe…


This script will count down from “startNumber” to 0 for “countSeconds” time.

public class Countdown : MonoBehaviour 
	public float startNumber = 100f;		// The number that we countDown
	public float countSeconds = 10f;		// Time needed for counting
	public float timer = 0f;

	private float timeMultiplier = 0f;		// Will multiply to deltaTime for counting

	void Start()
		timeMultiplier = startNumber / countSeconds;	// Get the number that we want to count / the seconds that we want to count it

	void Update()
		startNumber -= Time.deltaTime * timeMultiplier;	// Count
		timer += Time.deltaTime;	// Just to show the time passed from the begining

p.s. You can remove timer variable. It’s there just to show the time passed, so you can compare.