- Home /
Cannot retrieve score from previous scene/level
Hello, I am new in unity and I'm currently developing a space shooter shmup for my school project. I have a script that gets the current score and high score then saves it. Here it is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public int score;
public int BestScore;
public Text ScoreText;
// Use this for initialization
void Awake ()
{
ScoreText = GetComponent<Text> ();
BestScore = PlayerPrefs.GetInt ("bestScore");
score = 0;
}
// Update is called once per frame
void Update ()
{
ScoreText.text = "Score: " + score;
if (score > BestScore)
{
BestScore = score;
}
GameObject.Find("Score").GetComponent<Text>().text=""+score;
GameObject.Find("Best").GetComponent<Text>().text=""+PlayerPrefs.GetInt("bestScore");
}
public void OnDestroy()
{
PlayerPrefs.SetInt ("bestScore", BestScore);
PlayerPrefs.Save ();
}
}
Now the problem is that when I proceed to another scene, the current score returns to zero but the high score remains as it is which is fine but I need to retrieve the score from the previous scene to the current scene then continue gaining score as I destroy enemies just like in similar games. I have tried using DoNotDestroyOnLoad and PlayerPref.GetInt but it still doesn't work or perhaps I've made a mistake on my part. I have tried looking up for other solutions but still no luck so I thought I'd ask here. Thank you in advance.
DontDestroyOnLoad() should work. $$anonymous$$aybe you're resetting the score to zero from another script when you load different scenes?
You were right! I just found out that my game_manager script is resetting it to zero as I load to the next level. Here it is. score.GetComponent<Score$$anonymous$$anager>().score = 0;
But now I can't seem to figure out how I would transfer the score here and DontDestroyOnLoad() gives me a "DontDestroyOnLoad() only work for root gameobjects" message.
Answer by JedBeryll · Nov 22, 2017 at 05:38 AM
There are a few ways to do this. One is the DontDestroyOnLoad as jeffreyrampineda said, but it might cause problems if you designed your game differently. I think the best way for you is a static value.
public class ScoreManager : MonoBehaviour
{
public static int lastScore; //static value so it does not get reset when the manager is reloaded
public int score;
public int BestScore;
public Text ScoreText;
// Use this for initialization
void Awake ()
{
ScoreText = GetComponent<Text> ();
BestScore = PlayerPrefs.GetInt ("bestScore");
score = 0;
}
// Update is called once per frame
void Update ()
{
ScoreText.text = "Score: " + score;
if (score > BestScore)
{
BestScore = score;
}
GameObject.Find("Score").GetComponent<Text>().text=""+score;
GameObject.Find("Best").GetComponent<Text>().text=""+PlayerPrefs.GetInt("bestScore");
}
public void OnDestroy()
{
PlayerPrefs.SetInt ("bestScore", BestScore);
PlayerPrefs.Save ();
lastScore = score; //set lastScore at the end of the level
//you can now use this when another level is loaded
}
}
Note that this solution wont work if you quit the game. In that case you will have to save it in playerprefs too.
Thank you for the reply. Sadly, it still doesn't work for me but I want to try saving it on playerprefs but where would be the best place to put it?
Your answer
Follow this Question
Related Questions
New score each level 1 Answer
GetComponent doesn't work 2 Answers
Allow userto load custom unity levels/scenes. 2 Answers
Enter next level 1 Answer
How to set a game-over scene when an object hits another? 0 Answers