Hi, I'm making a driving game in which there are 6 zones. Each zone is accessed from the same scene and the high scores and best times are displayed under said zones.
This script is on the finish line trigger. I'm having a problem with the playerprefs and the saving of the best time. The time is being saved but it's always the time of the last lap that updates on the level select. I want it to keep the best time displayed and update if that time is beaten. Been trying all sorts of things but I'm pretty much at a stand still. Any help would be much appreciated.
var time2 : float = 0;
var finished : boolean = false;
var finishsound : AudioClip;
var GuiTime : GUIText;
function Awake(){
time2 = 0;
}
function Start(){
}
function Update(){ //Add the time every Frame if race isnt finished
if(finished == false){
time2 += Time.deltaTime;
}
}
function OnTriggerEnter(){
finished = true;
audio.clip = finishsound;
audio.Play();
yield WaitForSeconds (2);
CheckResults();
}
function OnGUI(){
if(finished == true){ //if Race finished
GUI.Label(Rect(Screen.width / 2 - 65, 100, 200, 40), "Your Time " + time2);
GUI.Label(Rect(Screen.width / 2 - 58, 120, 200, 40), "R to Improve");
}
else{ //if race isnt finished (only draw time)
GuiTime.text = time2.ToString();
time2 = Mathf.Round(time2 * 1000) / 1000;
}
}
function CheckResults (){
if(time2 < PlayerPrefs.GetFloat("QZ5Time")){
PlayerPrefs.SetFloat("QZ5Time", time2);
PlayerPrefs.Save();
}
if(EnemyDeathDL5.kills > PlayerPrefs.GetInt("QZ5kills")){
PlayerPrefs.SetInt("QZ5kills", EnemyDeathDL5.kills);
PlayerPrefs.Save();
}
Application.LoadLevel("level_select");
}
↧