- Home /
Local High Score with Game Over Scene
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 () {
}
}
Answer by Ali_Jaffer · May 26, 2017 at 11:32 AM
this is error in your code you are using wrong preference in GameOverScript line no. 19
highScore = PlayerPrefs.GetInt ("Score");
use the right preference as you saved them in HUDscript
highscore = PLyerprefs.GetInt("HighScore");
Your answer
Follow this Question
Related Questions
I need some help with my high score.,I need help with my highsore. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
My sphere is not moving in the unity? 1 Answer
Adding a delay for weapon switching 1 Answer