Hey, I am finding this one quite tough to code. For some reason it still doesn’t save the Highscores according to the scenes i am playing on
here is the code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;
public class ScoreManager : MonoBehaviour
{
public static int score = 0;
public Text scoreText;
public static int highScore = 0;
public Text highScoreText;
public Scene scene;
void Awake()
{
Scene scene = SceneManager.GetActiveScene ();
}
//Resets score and Loads highest score
void Start()
{
score = 0;
highScore = PlayerPrefs.GetInt ("highScore"+scene.name, 0);
}
//Updates all the goodness!!!
void Update()
{
if(score > highScore) {
highScore = score;
}
scoreText.text = "SCORE: " + score;
highScoreText.text = "HIGHSCORE: " + highScore;
}
//Saves highest score
void OnDisable() {
PlayerPrefs.SetInt ("highScore"+scene.name, highScore);
PlayerPrefs.SetInt ("score", score);
}
//Use with UI button to load score manually
public void LoadScore() {
highScore = PlayerPrefs.GetInt ("highScore"+scene.name, 0);
score = PlayerPrefs.GetInt ("score", 0);
}
//Use with UI button to save score manually
public void SaveScore() {
PlayerPrefs.SetInt ("highScore"+scene.name, highScore);
PlayerPrefs.SetInt ("score", score);
}
//Use with UI button to clear highScore
public void clearScore(){
//PlayerPrefs.DeleteAll();
if (PlayerPrefs.HasKey ("highScore"+scene.name)) {
PlayerPrefs.DeleteKey ("highScore"+scene.name);
setScore();
Debug.Log ("ClearedScore for "+scene.name);
}
}
//Resets highScore to 0, or current score if higher than 0
void setScore() {
highScore = 0;
//highScore = score;
}
}
I appreciate all help i get ![]()