- Home /
Unity3D Timer.
Hi,I have tried all sort of ways to make timer.NOTHNG.I need timer which from a particular time goes to zero,when it goes to the end-0,game ends (shows text: time over...) And then i can restart game: pressing restart.And when i finish round (''first'' level): TIMER STOPS.Help please.
Answer by aldonaletto · Jun 23, 2012 at 09:02 PM
You can attach this script to a GUIText, which can be used to show the remaining time and the message Time Over - create a GUIText, position it and add the script below:
var timer: float = 300; // set duration time in seconds in the Inspector
function Update(){ timer -= Time.deltaTime; if (timer > 0){ guiText.text = timer.ToString("F0"); } else { guiText.text = "TIME OVER\nPress X to restart"; if (Input.GetKeyDown("x")){ // reload the same level Application.LoadLevel(Application.loadedLevel); } } }
Its realy basic,i want something like that what i wrote,I dont have any idea how to do that.
Aldo's answer is what you asked for. Read through and see what is happening (I have spaced the lines and added your question as comments) :
var timer: float = 300; // set duration time in seconds in the Inspector
function Update()
{
timer -= Time.deltaTime; // I need timer which from a particular time goes to zero
if (timer > 0)
{
guiText.text = timer.ToString("F0");
}
else // timer is <= 0
{
guiText.text = "TI$$anonymous$$E OVER\nPress X to restart"; // when it goes to the end-0,game ends (shows text: time over...)
if (Input.Get$$anonymous$$eyDown("x")) // And then i can restart game: pressing restart.
{
Application.LoadLevel(Application.loadedLevel); // reload the same level
}
}
}
No thats not,game is not stopping,i need to press x then it will ONLY RESTART.
Seriously. If you want people just to do it for you for free then go somewhere else. People here have explained to you what to do. Given you code and guidelines. If you want a "do it for me now, exactly as I want it" answer then go somewhere else
Just realized that original question was 4 years ago. Still leaving the comment here to discourage people in the future to have this attitude
you have to tell the timer to stop if the level has been finished. I have added a boolean and a condition to check if the level has been completed or not.
var timer: float = 300; // set duration time in seconds in the Inspector
var isFinishedLevel : boolean = false; // while this is false, timer counts down
function Update()
{
if (!isFinishedLevel) // has the level been completed
{
timer -= Time.deltaTime; // I need timer which from a particular time goes to zero
}
if (timer > 0)
{
guiText.text = timer.ToString("F0");
}
else
{
guiText.text = "TI$$anonymous$$E OVER\nPress X to restart"; // when it goes to the end-0,game ends (shows text: time over...)
if (Input.Get$$anonymous$$eyDown("x")) // And then i can restart game: pressing restart.
{
Application.LoadLevel(Application.loadedLevel); // reload the same level
}
}
}
How you use a timer is depending on your code, but the essential theory is there :
// TI$$anonymous$$ER counting UP
var timer : float = 0.0;
var timer$$anonymous$$ax : float = 3.0;
function Update()
{
timer += Time.deltaTime;
if (timer >= timer$$anonymous$$ax)
{
Debug.Log("timer$$anonymous$$ax reached !");
// reset timer
timer = 0;
}
}
// TI$$anonymous$$ER counting DOWN
var timer : float = 0.0;
var timer$$anonymous$$ax : float = 3.0;
function Start()
{
timer = timer$$anonymous$$ax ;
}
function Update()
{
timer -= Time.deltaTime;
if (timer < 0)
{
Debug.Log("timer Zero reached !");
// reset timer
timer = timer$$anonymous$$ax;
}
}
Awesome,it works,you are super,now can you please help me to make game freezes when time is reached to zero,and shows something like : you are lost and i can press restart.And please help me to make timer shows in $$anonymous$$utes,like 2:00 not 200,well $$anonymous$$utes and frozen game is important thing,Please help me at this.I really appreciate your help!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Unity3D Game Time 1 Answer
How to tell if 2 blocks are next to each other in a 2d game? 1 Answer