Question by
rdgorodrigo · Sep 07, 2015 at 11:46 PM ·
uitimerscoretracking
Race saving best times on different scene
Hello I want to save my best times on a different scene using the UI, so far my scripts manage to stop the time al the goal, but the best time reamains 0. These are my two scripts. Thanks!
Timer:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Timer : MonoBehaviour {
public float time = 60;
public float tempTime;
public Text text;
public Goal goal;
void Update(){
if(goal.lap < 1){
tempTime += Time.deltaTime;
time = 60;
} else {
time = tempTime;
//to scores see highScoreTracker
}
text.text = "TIME : " + tempTime.ToString();
}
}
HiScoreTracker:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class highScoreTracker : MonoBehaviour {
public Timer timer;
public float highScore = 15f;
public Text text;
void Start(){
highScore = PlayerPrefs.GetFloat(Application.loadedLevelName);
if(highScore == 0){
highScore = 60;
text.text = "TOP TIME : NONE";
} else {
ChangeText();
}
}
void Update(){
Debug.Log(highScore);
if(timer.time < highScore){
highScore = Mathf.Round(timer.time * 100) / 100;
Save();
}
}
public void Save(){
PlayerPrefs.SetFloat(Application.loadedLevelName, highScore);
PlayerPrefs.Save();
print("saved");
ChangeText();
}
public void ChangeText(){
text.text = "TOP TIME : " + highScore.ToString("F2");
}
}
Comment