I am a beginner in Unity. I have a high score thing going on that uses survival time and PlayerPrefs. I run the game and the timer keeps blinking, and then the second time I run the game, the time just isn’t there. I’ve tried resetting the PlayerPrefs to 0. I’ve tried changing it so it compares the times using Time.deltaTime rather than referencing the other time script (I thought it might be because it has to update so frequently, but that wouldn’t explain the total lack of a high score displaying). Any ideas?
High score script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TimeHighscore : MonoBehaviour {
private float highscore;
private float score;
private Timer theTime;
public Text highscoreText;
// Use this for initialization
void Start () {
theTime = FindObjectOfType <Timer> ();
//PlayerPrefs.SetFloat ("TimeHighscore", 0);
if (PlayerPrefs.HasKey ("TimeHighscore")) {
highscore = PlayerPrefs.GetFloat ("TimeHighscore");
} else {
highscore = 0;
}
highscoreText.text = "Time Highscore: " + highscore;
}
// Update is called once per frame
void Update () {
score = theTime.timer;
if (score > highscore) {
highscore = score;
highscoreText.text = "Time Highscore: " + highscore;
PlayerPrefs.SetFloat ("TimeHighscore", highscore);
}
}
}
Timer script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour {
public float timer;
public Text timerText;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
timerText.text = "Time Survived: " + timer;
}
}