Getting the score in Main Menu from static ScoreManager from other scene

Hi,

I have a static ScoreManager script handling the score in the GamePlay scene. The static ScoreManager script is attached to an empty GameObject in the GamePlay scene.

Now I wish to get the highScore from ScoreManager to be loaded in the Main Menu. How can I get the highScore in the MainMenu out of the static ScoreManager script on game start?

For now I’m able to get the highScore in the main menu, but only after I played the main gamePlay scene and go back to the main menu again.

Sorry, still new.

Just out of curiousity, can you post the ScoreManager?
When you say it’s static, do you mean a variable is static or the class?
I wonder if the variable is only created when you load the other scene, and that’s why you can’t access it the first time around…

Thanks for the information - didn’t know that. Post was edited.

  • here stood bullshit, as it seems that scriptableobjects can not correctly be used in builds-

Please don’t do that, as ScriptableObjects only behave that way in the editor :slight_smile:
You cannot use them (like that) in a build…

Yeah sure, this is the ScoreManager. A part of it gets handled by the GameOverManager, which is also on an Empty Object insode the GamePlay scene.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ScoreManager : MonoBehaviour {


    public static ScoreManager instance;

    private Text scoretext;
    private Text highScorePreText;
    private Text highScoreText;

    private GameObject scorePreObjText;
    private GameObject highScoreObjText;


    private int score;
    private int highScore;
    private int prevHighScore;

    string highScoreKey2 = "HighScore";

    private Text coinScoreText;
    private int coinScoreCount;


    void Awake ()
    {

        // Find the gameObject of the text ellement
        scorePreObjText = GameObject.Find("High Score Pre");
        highScoreObjText = GameObject.Find("High Score");

        // Find the text ellement
        scoretext = GameObject.Find ("Score Text").GetComponent<Text> ();
        highScorePreText = GameObject.Find ("High Score Pre").GetComponent<Text> ();
        highScoreText = GameObject.Find ("High Score").GetComponent<Text>();
        coinScoreText = GameObject.Find ("Score Text Coin").GetComponent<Text> ();

        //Loading in the scores from latest round
        highScore = PlayerPrefs.GetInt(highScoreKey2, highScore);
        prevHighScore = PlayerPrefs.GetInt(highScoreKey2, highScore);

        MakeInstance();


        highScoreObjText.SetActive (false);
        scorePreObjText.SetActive (false);

    }

    void MakeInstance () {
        if (instance == null)
            instance = this;
    }


    public void IncrementScore () {
       
        score++;
        scoretext.text = "" + score;
        //Debug.Log ("Score called: " + score);
    }


    public void IncrementCoinScore ()
    {
        coinScoreCount++;
        coinScoreText.text = "" + coinScoreCount;
    }

    public void HighScore ()
    {



        if (score > highScore) {
            highScore = score;
            PlayerPrefs.SetInt (highScoreKey2, highScore);
            PlayerPrefs.Save ();

        }

        highScorePreText.text = "New High";
        highScoreText.text = "" + highScore;


    }


    // Also part of the score. Helps the GameOverPanel to get the HighScore from previous round.
    public void UpdatePrev ()
    {
        prevHighScore = highScore;
        }


    // ----- Sends info to GameOverManager class for the GameOverPanel() function ------

    public int GetHighScore ()
    {

        return this.highScore;
    }

    public int GetPrevHighScore ()
    {


        return this.prevHighScore;

    }

    public int GetCoinScore ()
    {
        return this.coinScoreCount;   
    }


    public int GetScore ()
    {

        return this.score;
    }

    // ---------------------------------------------------------------------------------
}

Okay, so it’s a variable (singleton) that is static. You cannot access that because it is only assigned (first) when you load that particular scene.
For your situation, though, it’s not that important, because you can fetch the score from playerprefs; I’d use that to display the score or look it up (whatever you’re doing) on the main menu.

1 Like

Can you send me into the right direction how to do this?

I could but you have an example in your ScoreManager, already :slight_smile:

int score = PlayerPrefs.GetInt("HighScore");

It can be so simple. Sorry, still finding my way into c#! :slight_smile:

:slight_smile: