I’ve used a different scene for the game over screen in the quiz section. How do i access the score from the previous scene?
Is it even worth it to use a different scene for game over?
You can’t ‘just access’ scores from the previous scene. Those no longer exist. But there are some options;
- Keep a
ScoreManager
type object where the score is stored. Put this object inDontDestroyOnLoad
. It will persist across scenes
public vlass ScoreManager : MonoBehaviour
{
private int _score = 0;
private void Awake()
{
// how many objects do we have?
int count = FindObjectsOfType<ScoreManager>().Length;
if (count == 1)
{
// only one, put it in the DDoL
DontDestroyOnLoad(gameObject);
}
else
{
// too many. destroy this one
Destroy(gameObject);
}
}
public void SetScore(int score)
{
_score = score;
}
public int GetScore()
{
return _score;
}
}
Now you can set and get the score using FindObjectOfType<ScoreManager>()
.
- Save the score to
PlayerPrefs
and then just read it again on the next scene
// Save the score
PlayerPrefs.SetInt("Score", _score);
// Read the score
int _score = PlayerPrefs.GetInt("Score", 0); // 0 is returned if no score was saved
These are a few that should help
What is playerprefs?
It’s a built in mechanism for storing player preferences. Small things like score
1 Like
ok thanks. I thought it meant prefabs
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.