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?