I have an endless game where the score increases based off of Time.fixedDeltaTime. I’m trying to store a high score in player preferences if it is higher than the earned score but it doesn’t seem to be sticking. Can someone point out what I’m doing wrong here? I’m storing scores in a HUDScript and presenting score and high score in game over script.
HUDScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HUDScript : MonoBehaviour {
float playerScore = 0;
public Text scoreText;
void Start () {
Screen.sleepTimeout = SleepTimeout.NeverSleep;
Screen.orientation = ScreenOrientation.LandscapeLeft;
}
void Update () {
playerScore += Time.fixedDeltaTime;
scoreText.text = "Score: " + Mathf.Floor((playerScore * 100)).ToString();
}
public void IncreaseScore(int amount){
playerScore += amount;
}
void OnDisable() {
PlayerPrefs.SetInt ("Score", (int)(playerScore * 100));
if (PlayerPrefs.GetInt("HighScore") <= (int)playerScore)
PlayerPrefs.SetInt("HighScore", ((int)playerScore));
}
}
GameOverScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScript : MonoBehaviour {
HUDScript hud;
int score = 0;
int highScore = 0;
public Text scoreText;
public Text highScoreText;
// Use this for initialization
void Start () {
score = PlayerPrefs.GetInt ("Score");
scoreText.text = "Score: " + score.ToString ();
highScore = PlayerPrefs.GetInt ("Score");
highScoreText.text = "High Score: " + highScore.ToString ();
}
void Update () {
}
}