High-score Saving Issue
I want to implement a high score system in my dodge game, but I can't seem to make it work. Currently, I have a score, which starts from 500,000 and gets down 150 points per seconds. This works, although when I die it still goes on, which I need to fix. I have a second kmHighscore Text, and I want it to show the highscore. Right now it shows the exact score on which I died(for example 450,875), but when I restart the game I can't seem to make it be saved. I tried with PlayerPrefs, but I am not sure how to apply it in my code. I want it to represent the lowest, in this case as it starts from 500,000, score ever achieved. Just to note: Both kmScore and kmHighscore are just numbers, I don't have text before the score or the highscore in the Unity window. Any help will be appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameOver : MonoBehaviour {
public GameObject gameOverScreen;
public Text kmScore;
public Text kmHighscore;
float savedScore;
bool gameOver;
private float score = 500000;
void Start () {
FindObjectOfType<PlayerController>().OnPlayerDeath += OnGameOver;
}
public void Update () {
kmScore.text = GetScore().ToString("F0");
if (gameOver)
{
if (Input.GetKeyDown (KeyCode.Space))
{
SceneManager.LoadScene(1);
}
}
}
float GetScore()
{
return score - (float)Time.timeSinceLevelLoad * 150;
}
void OnGameOver()
{
gameOverScreen.SetActive (true);
kmHighscore.text = GetScore().ToString("F0");
}
}
Answer by Cuttlas-U · Feb 11, 2018 at 02:46 PM
hi; there are some things need to be done to fix this ;
first of all calculating the High Score should be only when its not game over so u should change this part in the update method :
then u need to save the score when u die and load it when the game starts again ;
to save the score u need to have another variable to store the last highscore cause u dont access that function when u die;
and at last u need to check if the current score is not more then your last saver high score ;
so it all changes like this
CurrentHighScore = 0; void Start() { FindObjectOfType().OnPlayerDeath += OnGameOver;
if ( PlayerPrefs.HasKey("HighSScore"))
{
CurrentHighScore = PlayerPrefs.GetFloat("HighSScore");
kmScore.text = CurrentHighScore.ToString("F0");
}
}
float CurrentScore;
public void Update()
{
if (gameOver)
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (CurrentScore > CurrentHighScore)
{
PlayerPrefs.SetFloat("HighSScore", HighestScore);
}
SceneManager.LoadScene(1);
}
}
else
{
CurrentScore = GetScore();
if ( CurrentScore > CurrentHighScore)
{
kmScore.text = GetScore().ToString("F0");
}
}
}
float HighestScore;
float GetScore()
{
HighestScore = score - (float)Time.timeSinceLevelLoad * 150;
return HighestScore;
}
i did this really quick so they mayebe some problem tell me about them if u see;
Answer by codeblue00 · Feb 11, 2018 at 06:45 AM
This is a good tutorial for easily implementing a save/load function with JSON. if you want to use player prefs I'm pretty sure this tutorial covers saving/loading with that option.
Not really helpful. I have used PlayerPrefs. the problem is that I can't implement it with my code.
Your answer
Follow this Question
Related Questions
Highscore not working 0 Answers
Modification to Unity2D Roguelike 1 Answer
Player character jumping when I wish to only initiate pause - UI Button element. 1 Answer
Prefab Direction - shooting 2 Answers
2D Collision Best Practice 0 Answers