I am making a collecting objects game and I need to know how I can display how long it took someone to complete a level after they have collected all of the items. How can I do this.
Here is an example script (C#) (attach it to a GUI Text in the scene (Game Object > Create Other > GUI Text)):
public float timerAccuracy = 0.1f; //How frequently will the timer update? Time in seconds.
public bool timerActive = ture; //Set this to false when you want to stop the timer
public string textBeforeSeconds = "Time: "; //If you don't want the text before the timer, leave it blank.
public string textAfterSeconds = " seconds"; //If you don't want the text after the timer, leave it blank.
private float currentTime; //The actual time since timerAcive was true
IEnumerator Start () {
currentTime = 0; //Time is set to 0 at the start
//Set the appropriate text to the GUI Text
guiText.text = timeBeforeSeconds + currentTime + timeAfterSeconds;
while (true) { //Will loop, and preform much like Update()
if (timerActive) {
//Add (timerAccuracy) to the actual time, every (timerAccuracy) seconds
yield return new WaitForSeconds(timerAccuracy);
currentTime += timerAccuracy;
guiText.text = timeBeforeSeconds + currentTime + timeAfterSeconds;
}
yield return null;
}
}
If you need a JavaScript version, I will update the answer.