- Home /
timer starts in the menu scene
Hi! i am middle of something... what is happening in my game, i have two scenes, scene1 and scene2 , i have a script of timer which actually is the score which should start when i load the scene2 as in the temple run ,subway surfer etc. when i play my game first it show the menu scene then we hit play and move to scene2, now what is happening when game load scene1 starts, my timer score starts counting time score in the scene1 although my script and score gui is in the scene2. and when my player collide and we go back to scene1 the score is running and after again loading the scene2 the scores are like 1000....1950..what i want to just to start my score in the scene2 and after reloading scene2 start form the zero. #pragma strict
private var startTime:float;
// var labelPosition : Rect;
var labelText : String;
var labelStyle : GUIStyle;
var score : int;
static var timerstart:boolean = true;
function Update()
{
if (timerstart)
{
startTime= Time.deltaTime;
//startTime=
labelText = startTime.ToString();
}
}
function OnGUI()
{
GUI.Label(Rect (Screen.width-125, 30, 130, 25), labelText, labelStyle);
}
Answer by valerik · Feb 21, 2014 at 12:09 PM
maybe you need PlayerPrefs
Full details are here: http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
use something like this:
static var timeSpentInSceneX: float;
function saveTimer(){
PlayerPrefs.SetFloat("timerSceneX",timeSpentInSceneX);
}
function getTimer(){
storedTimer = PlayerPrefs.GetFloat("timerSceneX");
}
and then use some operation to modify your timer value! ex:
timeSpentInSceneX = Time.deltaTime;
function saveTimer();
(maybe Time.time it's better, it's frame indipendent)
then you store your time as above, when you load a new scene you get the old stored value and you re-do:
timeSpentInSceneX = Time.deltaTime;
float totalTime = timeSpentInSceneX + getTimer();
Thanks for your response @valerik ... should i apply this script in the scene2 timer empty object , bacause i have applied my above script in the same manner. and i am using time.time now. and how in your script i will apply the GUI.label to show the time.... kindly write that in your code. i am very new to unity and js coding. in my script if i change startTime:int and deltatime into time.time it shows time in the scene2 well, but as i mentioned the timer starts in the scene1. this is the main concerned ... i want it start it in the scene2.. :( ... kindly help .. and playerpref is furute work but if i cover it in here i ll be more glade .. bacuase i will have to save the time as well to show on the scene3 gui..... ;p
Your answer
Follow this Question
Related Questions
Time.deltaTime returns smaller values than actual time between frames 0 Answers
How do I make the timeScale not affect the timer? 1 Answer
Pause & Unpause Different timers 1 Answer
How to limit an effect to a certain duration 2 Answers
Delay Player From Using Action Button For A Period Of Time 0 Answers