- Home /
Time.deltaTime High Score and Score count gives high score, but carries on counting it after saving with playerprefs
Hi, I am making a survival run game for wp8. I have got a timer which works efficently with Time.deltaTime,and a high score that saves after I load my death level. However; I have two questions. The first is when I respawn, the highscore shows my previous time but still counts up on the timer. Secondly, the high score resets after quitting and loading the app. Here are the scripts.
var IncreaseTime : boolean = true;
static var Timer = 0.0;
static private var highScore : float;
var collectedTime : boolean = true;
static public function AddPoint() {
if(Timer > highScore) {
highScore = Timer;
}
}
function Update ()
{
if(IncreaseTime == true)
{
Timer += Time.deltaTime;
guiText.text = "" + Timer;
}
if(Timer > highScore) {
highScore = Timer;
}
if(IncreaseTime == false){
Timer += Time.deltaTime;
guiText.text = "" + Timer;
}
if(collectedTime == true){
}
}
function Start() {
highScore = PlayerPrefs.GetFloat("highScore", 0);
collectedTime = true;
}
function OnDestroy() {
PlayerPrefs.SetFloat("highScore", highScore);
{
IncreaseTime = false;
Debug.LogError("lol unity is giving you errors for no reezon!");
}
}
#pragma strict
var TimeKeeper : float;
function Start () {
}
function Update () {
TimeKeeper += Time.deltaTime;
guiText.text = "" + TimeKeeper;
}
Answer by NoseKills · May 11, 2014 at 03:54 PM
Timer and highScore are static variables. Anything you declare static is not specific to an instance of the class and so it doesn't get reset when your GameObjects and scripts are destroyed and recreated as you load/reload the scene you are counting the score in.
You need to manually reset the static variables that need to be reset for example in the Start method of one of your game mode's classes.
Answer by MorphiusX · May 11, 2014 at 03:57 PM
I fixed this myself recently by just telling it to set the variables to 0 once game had finished. Im not sure about the second issue
Your answer
Follow this Question
Related Questions
timer starts in the menu scene 1 Answer
Deal damage as player moves. need help! 0 Answers
How do you make an Accurate Timer on iphone 1 Answer
Time.deltaTime returns smaller values than actual time between frames 0 Answers
Countdown timer using Time.unscaledDeltaTime not working as expected. 1 Answer