Hello Everyone,
I’m creating a survival game where a timer displays how long the player is currently surviving. The timer is working perfectly however I’m having some difficulties on saving it whenever the player gets destroyed and survives the longest.
I did some research and found out that using playerprefs happens to be the best method for saving locally, therefore I wrote with the following script.
#pragma strict
var textTimer : GUIText;
var timer : int = 0; // start timer
var minutes : float = 0;
var seconds : float = 0;
var stoptime : float = 0;
var normaleSize : float = 20;
var newSize : float = 30;
var changefont : float = 0;
var time : float = 0;
var increaseSize : boolean = false;
var timeActive : boolean = true;
var asteroid : GameObject;
var scoreText : GUIText;
function Update () {
if (increaseSize == true) {
time += Time.deltaTime;
// this basically gets the normal size of the text and changes it to the new size
// which is achieved by getting the value of time and dividing it by the font size
textTimer.fontSize = Mathf.Lerp ( normaleSize, newSize, time / changefont);
}
textTimer.text = "Timer: " + minutes + ":" + seconds;
if (timeActive) {
timer = Time.timeSinceLevelLoad;
minutes = (timer / 60); //
seconds = (timer % 60);
}
}
function StopTimer () {
timer = 0;
timeActive = false;
print ("time stoped!");
yield WaitForSeconds (3);
animation.Play();
}
function FontSize () {
increaseSize = true;
//textTimer.fontSize = 30;
//animation.Stop ();
}
function HighscoreUpdate () { // Here's the problem
if ( timer > PlayerPrefs.GetInt("TimeScore")) { // If timer value happens to be greater then the saved one, then... ---- This isn't working ----
PlayerPrefs.SetInt ("TimeScore", timer); // captures the current time and saves it
NewScore (); // This functions displays the capture time in a GUIText
}
//NewScore (); // By enabling this, the script works***
}
function NewScore () {
scoreText.text = "Highscore: " + minutes + ":" + seconds + PlayerPrefs.GetInt ("TimeScore"); // the capture time is display on a GUIText
}
Unfortunately my timer isn’t being saved nor displayed, I don’t have a single idea why this is happening. I basically wanted that if timer happens to be greater then the previous saved time… then save the new time and display it as a new highscore.
However if I were to enable the //NewScore ();
, then the time is displayed in the highscore, but restarting the level causes it to reset every time.
Does anyone have an idea on how to fix this problem? Any help is highly appreciated!