- Home /
DontDestroyOnLoad reset the gameobject
Hi,
I'm having a flappy bird kind of game. I would like to have previous score shown on the screen when I die and start it all over. Somehow I don't manage to do so. However the best scores shows at it should (and all the other text element).
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameControlScript : MonoBehaviour
{
public Text scoreText;
public GameObject gameOvertext;
public Text lastScoreText;
public Text bestScoreText;
int bestScore = 0;
int score = 0;
bool isGameOver = false;
void Awake()
{
bestScore = PlayerPrefs.GetInt ("best", 0);
}
void Update()
{
//if the game is over and the player has pressed some input...
if (isGameOver && Input.anyKey)
{
//...start a new game.
Application.LoadLevel(Application.loadedLevel);
}
bestScoreText.text = "Best: " + bestScore;
}
public void Scored()
{
score++;
scoreText.text = "Score: " + score;
}
public Died()
{
gameOvertext.SetActive (true);
isGameOver = true;
lastScoreText.text = "LAST: " + score;
//check if high scored
if (score >= bestScore)
{
PlayerPrefs.SetInt("best", score);
PlayerPrefs.Save();
}
}
}
Then again I have an extra script for lastScoreText only which is named NoDestroy:
using UnityEngine;
using System.Collections;
public class NoDestroy : MonoBehaviour {
void Awake (){
DontDestroyOnLoad (this.gameObject);
}
}
but it still reset my "last score" when I restart the level. I don't know why, but appreciate if someone could help me. I have also tried to put empty gameobject and place lastScoreText there, but the results are same. So the question is: How I can modify the code so it won't reset my lastScoreText when restarting the level?
Answer by nullgobz · Jul 31, 2015 at 04:12 PM
Well your best score is using player prefs to save the score to the registry. So do the same with your last score?
public class GameControlScript : MonoBehaviour
{
int lastScore = 0;
}
void Awake()
{
lastScore = PlayerPrefs.GetInt ("last", 0);
}
public Died()
{
gameOvertext.SetActive (true);
isGameOver = true;
lastScoreText.text = "LAST: " + lastScore;
PlayerPrefs.SetInt("last", score);
//check if high scored
if (score >= bestScore)
PlayerPrefs.SetInt("best", score);
PlayerPrefs.Save();
}
Thanks for the reply.
Yes, that's a one option, but I thought it might be easier use just DontDestroyOnLoad.
Should I also save it (PlayerPrefs.Save) after setting the last score? Does that disturb also the best score field? Or would it replace only the changed field?
Is there a difference with the performance using PlayerPrefs or DontDestroyOnLoad?
Thanks again.
Yes if your using DontDestroyOnLoad you dont really need playerprefs. But since you started with playerprefs i thought you wanted to use it. PlayerPrefs.Save() will save all the values you have set, so you only need to call it once. I would use playerprefs only if you need to save the score across diffrent ga$$anonymous$$g sessions. And use the DontDestroyOnLoad method if you dident need to see the "last score" next time you start the game. About the performance im not sure.
Your answer
![](https://koobas.hobune.stream/wayback/20220611233858im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Guide in ArrayPrefs or PlayerPrefx? 1 Answer
keep adding to playerprefs 1 Answer
score broken, score loads on new scene but then errors 0 Answers
GetComponent doesn't work 2 Answers
PlayerPrefs Highscore problem 1 Answer