- Home /
High score system with timer?
Hello! So basically I have a little timer which is displayed on every scenes and counts up from 0. The timer resets every scene, so basically you can time how quickly you finished the level. However, right now the timer literally has no function other than counting. I'd like to make it so at the end of each level it stores the time you got and then somehow display it on the last level, any ideas on how I'd go about doing this? Here is my script:
public Text counterText;
public float seconds, minutes;
// Use this for initialization
void Start () {
counterText = GetComponent<Text>() as Text;
}
// Update is called once per frame
void Update() {
minutes = (int)(Time.timeSinceLevelLoad/60f);
seconds = (int)(Time.timeSinceLevelLoad % 60f);
counterText.text = minutes.ToString("00") + ":" + seconds.ToString("00");
}
Answer by ShroomWasTaken · Dec 27, 2017 at 01:06 AM
I would probably use PlayerPrefs to do this.
Here's what I would do:
1. When a scene is finished, save the time it took using PlayerPrefs.
2. When a new scene is started, load that PlayerPref to a new variable, let's call it lastSceneTime.
3. Display that variable to the screen using a GUI Text.
Thanks! This is certainly the solution. I'm kind of confused on saving the time though, what do you think would be the best method to go about doing this? Could I possibly make an onTriggerEnter that saves the time? Then how do I load it to a new variable? I'm sorry for all the questions but I've never actually used PlayerPrefs.
Well, I'd probably do something like this:
private float seconds;
private float $$anonymous$$utes;
[SerializeField]
private Text counterText;
[SerializeField]
private Text lastCounterText;
private void Start()
{
if (lastCounterText != null)
{
lastCounterText.text = PlayerPrefs.GetFloat("$$anonymous$$inutes").ToString() + ":" + PlayerPrefs.GetFloat("Seconds").ToString();
}
}
private void OnDestroy()
{
PlayerPrefs.SetFloat("Seconds", seconds);
PlayerPrefs.SetFloat("$$anonymous$$inutes", $$anonymous$$utes);
PlayerPrefs.Save();
}
Answer by ShadyProductions · Dec 27, 2017 at 01:05 AM
use PlayerPrefs for this
// Setting them
PlayerPrefs.SetInt("Minutes", minutes);
PlayerPrefs.SetInt("Seconds", seconds);
// Getting them
var minutes = PlayerPrefs.GetInt("Minutes");
var seconds = PlayerPrefs.GetInt("Seconds");
It lasts through scenes.