- Home /
Problem stopwatch start the game.
Hello to all!
I am new to scripting. When I try the game finished, in the menu I select play game now everything is ok, but when the game starts the clock has already started scoring 1 or 2 minutes already elapsed. I would like the stopwatch 0:0:0 departed from, when I press play on the menu, how to solve this problem?
stopwatch script I am using.
var start;
var tempoRimanente:int;
var millisecondi:int;
var secondi:int;
var minuti:int;
function Start() {
start=120; //secondi;
}
function Update () {
tempoRimanente= start = Time.time;
minuti = tempoRimanente/60;
secondi = tempoRimanente%60;
millisecondi = tempoRimanente%10;
if(tempoRimanente<0)
guiText.text="GAME OVER";
else
guiText.text= minuti.ToString("D2") + ":" + secondi.ToString("D2") + ":" % millisecondi.ToString("D2");
}
Answer by Drakulo · Jan 27, 2014 at 09:25 AM
The readonly Time.time var contains the time elapsed since you started Unity3D engine. So to manage your level time, you have to do some more code.
When your press the "Play" button, store the actual elapsed time in your script. This will be your start point :
var levelStartTime = Time.time;
Now, include this var in your time computation. The remaining level time will be :
var remainingTime = start + levelStartTime - Time.time;
Thank you for having responded to me!
I do not understand a lot of scripts. :-( Where dorei insert these variables in the script?
I'll show you how I did. so is it wrong? Please help me!
var levelStartTime = Time.time;
var remainingTime = start + levelStartTime - Time.time;
var start;
var tempoRimanente:int;
var millisecondi:int;
var secondi:int;
var $$anonymous$$uti:int;
function Start() {
//levelStartTime=Time.time; //secondi;
}
function Update () {
tempoRimanente= start = Time.time;
$$anonymous$$uti = tempoRimanente/60;
secondi = tempoRimanente%60;
millisecondi = tempoRimanente%10;
if(tempoRimanente<0)
guiText.text="GA$$anonymous$$E OVER";
else
guiText.text= $$anonymous$$uti.ToString("D2") + ":" + secondi.ToString("D2") + ":" % millisecondi.ToString("D2");
}
Ok let's keep it simple then, in only one script. This may not be the best solution but it's simple.
You will have a boolean storing if the game is on or off. When the flag is true, the timer will go on. When it's false, the menu is shown. The objective is to set the start time when the player click the start button (I guess you're using the unity GUI system). By clicking on the button, the start time will be set up and the playing flag set to true. The Update function manages your game logic until the player win or until the timer goes out. When the game is over, just flip the playing flag back to false.
var levelTimer = 120; // The level timer
var playing = false; // Not playing by default
var levelStartTime = 0f; // The current level start time
function OnGUI(){
if(!playing){
// The GUI is shown only if the game is not started
// Fill this rect with your button position & size
var buttonPosition = new Rect(x, y, width, height);
if(GUI.Button(buttonPosition, "Play")){
// Here you have to initialize the start time
levelStartTime = Time.time;
playing = true;
}
}
}
function Update(){
if(playing){
// Compute the remaining time
var remainingTime = levelStartTime + levelTimer - Time.time;
// Add your game loop code here
// Replace theGameIsOver by your gameover conditions
if(theGameIsOver){
playing = false;
}
}
}
Thank you Drakulo!
Are you the number one scripter. Thank you so much! :-)
I don't think so but... thanks. Don't forget to set the answser as accepted. Happy gamedev with Unity3D !
Your answer
Follow this Question
Related Questions
Change Scene after time? 1 Answer
Calling a function or Object in a level 1 Answer
Change scenes from area? 1 Answer
UI Button deletes Function on start 1 Answer
Objects are visible through terrain 3 Answers