Hi everyone, sorry but im noob on this… How can i stop the timer when 00:00:00 occurs? because the it keeps on repeating from 01:59:00 i just want to stop the time when it comes to 00:00:00 … thanks guys
here’s the script:
using UnityEngine;
using System.Collections;
public class TimerScript: MonoBehaviour {
public GUIText timer;
float minutes = 1;
float seconds = 0;
float miliseconds = 0;
void Update(){
if(miliseconds <= 0){
if(seconds <= 0){
seconds = 10;
}
else if(seconds >= 0){
seconds--;
}
miliseconds = 100;
}
miliseconds -= Time.deltaTime * 100;
//Debug.Log(string.Format("{0}:{1}:{2}", minutes, seconds, (int)miliseconds));
timer.text = string.Format("{0}:{1}:{2}", minutes, seconds, (int)miliseconds);
}
}
Here am giving small example with 5 minutes timer…
using UnityEngine;
using System.Collections;
public class GameTimer : MonoBehaviour
{
public static float Game_Seconds = 59;
public static float Game_Minutes = 4;
void Update()
{
if(Game_Seconds <= 0)
{
Game_Seconds = 59;
if(Game_Minutes >= 1)
{
Game_Minutes --;
}
else
{
Game_Minutes = 0;
Game_Seconds = 0;
GameObject.Find("Timer").guiText.text = Game_Minutes.ToString("f0") + ":0" + Game_Seconds.ToString("f0");
}
}
else
{
Game_Seconds -= Time.deltaTime;
}
if(Game_Minutes == 0)
{
Application.LoadLevel ("Game_Over");
}
if(Mathf.Round(Game_Seconds)<= 9)
{
GameObject.Find("Timer").guiText.text = Game_Minutes.ToString("f0") + ":0" + Game_Seconds.ToString("f0");
}
else
{
GameObject.Find("Timer").guiText.text = Game_Minutes.ToString("f0") + ":" + Game_Seconds.ToString("f0");
}
}
}
-Prasanna