Saving Hi-Scores for Separate Scenes problem.

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 :confused: 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 :stuck_out_tongue:

It’s common that people forget…

PlayerPrefs.Save();

Damn, okay. That should work on it’s own? also where should it go in the script XD sorry for a noobish question XD

Chances are the lack of save isn’t your issue, however you should add that after you use SetInt. Unfortunately, you didn’t really explain what was going wrong. Are the playerprefs not being created? Is the wrong score being saved?

It does look like you’re trying to save score with buttons(maybe for testing purpose). Are the methods working? Did you put debugs in them to see if they were being called properly?

Also I will comment that updating stuff in Update like you are using is not necessary. Do these values change every frame? I doubt that. You should only update that stuff when it’s required.

ah, forgot to take it off as everytime you kill an enemy the score goes up, however, it saves the highscore on one of my maps but then when i change to go to another one the highscore is the same as the previous map… if that makes sense so basically i want separate highscores for different scenes

Both your score and highscore are static. However, I don’t know how you’re setting up your score to begin with. How are you adding points to score? Is scoremanager in each scene on it’s own or is it on a dontdestroyonload gameobject?

Are you resetting your score? (since they are static, they will maintain the same value across scenes)

normal score always resets however, the highscore will maintain the same score on both scenes and as for the other question yeah it’s in both scenes and in the npc’s attack script the points add up from there

FYI
scene in line 12 is a different scene than in line 17!

Good catch. Line 17 is a local variable and will not assign a value to your global variable.