1
\$\begingroup\$

This question already has an answer here:

I have the following script which I use for calculating the score

public class Score : MonoBehaviour {

    Score sc;

    void Start()
    {
        GameObject obj = GameObject.Find("ScoreSystem");
        sc = obj.GetComponent<Score>();

    }

    void OnTriggerEnter2D(Collider2D coll)
    {
        if (coll.tag == "Ball")
        {
            sc.score++;
        }
    }
}

and this is the class where I increase the value

public class Score : MonoBehaviour {

    public int score;

    void Start()
    {
        score = 0;
    }

}

But the problem I have is that the value always stays 0 when I switch to a different scene. I even used this code to store the value and display in the next scene but it didn't work

void Update()
{
    PlayerPrefs.SetInt("score", score);
    PlayerPrefs.Save();
}

and this code for taking the value

score = PlayerPrefs.GetInt("score");

How can I store the latest value inside an Int and display it in the next scene and when I leave that scene it reverts back to 0?

\$\endgroup\$

marked as duplicate by Philipp, Tyyppi_77, Alexandre Vaillancourt, Engineer, Gnemlock Jul 15 '17 at 2:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ If you recreate the Score component then yes, it will be 0 because you are setting it to 0 in your Start function. Also don't call player prefs in your update. Just save it at the end of the scene. \$\endgroup\$ – Sidar Jul 14 '17 at 12:10
0
\$\begingroup\$

Save to Disk: Use a serialisation library like the built in BinaryFormatter class or a 3rd party library like Newtonsoft's JSON.Net which you can get via NuGet in most IDEs. This will allow you to save the score to disk which you can load in the start method of your "Score" class if the save file exists.

This would allow you to keep the score without having to change much code and any possible references that you have set up in the editor but would mean that you have to manage the file between games as by default a new game would have the score of the last (easy to solve by deleting the file when starting a new game).

Keep the score object between scenes: In unity it is possible to call "DontDestroyOnLoad" which will preserve your "Score" Object when you go to the next scene. If you have another "Score" object in the other scene there will be two (one from the last scene and a new one from the new scene) so you might have to manage which one is used.

If you only want one "Score" you might want to look at implementing the Singleton pattern Unity have a video on it and there is also a good example here that you can use. The Singleton ensures that there is only one instance of an object and (usually) provides easy access to the current instance. You would have to change the GameObject.Find("ScoreSystem"); to something along the lines of Score.Instance which is more efficient when you have lots of objects.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.