I’ve tried a number of different things and I can’t figure this one out either. Now I’m just confusing myself and doing more harm than good, and I was hoping someone else could lend a hand. I figured out how to save total time in this thread, http://forum.unity3d.com/threads/241060-Help-With-Saving-Time-Score-in-Player-Prefs-c , but now I’d like to save the Best / Highest Time as a high score and I can’t seem to figure it out. Can someone point me in the right direction using c#, I’ve tried a number of things that I found in threads here and I know I’m doing something wrong.
Here is my code:
using UnityEngine;
using System.Collections;
public class DistanceCS : MonoBehaviour {
public float runTime; ;
public float bestTime;
void Start() {
PlayerPrefs.GetFloat ("runTime", runTime);
PlayerPrefs.GetFloat ("bestTime", bestTime);
if (PlayerPrefs.GetFloat ("runTime") > bestTime) {
bestTime = runTime;
} else
bestTime = bestTime;
}
void OnDestroy() { // Set when my player dies
PlayerPrefs.SetFloat ("runTime", Time.timeSinceLevelLoad);
PlayerPrefs.SetFloat ("bestTime", bestTime);
//var savedTime = PlayerPrefs.GetFloat ("runTime");
//PlayerPrefs.SetFloat ("runTime", savedTime + Time.timeSinceLevelLoad);
//Debug.Log (PlayerPrefs.GetFloat ("runTime"));
// ^ I had used these before to save the total time
}
void OnGUI () {
guiText.text = "Time: " + Time.timeSinceLevelLoad.ToString ("F2") + "\nBest Time: " + PlayerPrefs.GetFloat ("bestTime");
}
}