- Home /
Trouble with resetting the score with button
Im having trouble resetting the score when i load my scene again with a button. i looked up for solutions but none for my game. The score just remains the same after the first game play. I’m also using the same scene.
Button script
public void reload()
{
SceneManager.LoadScene("Game");
}
Score script
Text score;
public static int bestScore;
public Text bestScoreText;
void Start ()
{
score = GetComponent<Text>();
bestScore = PlayerPrefs.GetInt("Best");
}
void Update ()
{
if (Enemy.Counter > bestScore)
{
bestScore = Enemy.Counter;
PlayerPrefs.GetInt("Best", bestScore);
}
score.text = "You Dodged " + Enemy.Counter;
bestScoreText.text = "Best " + bestScore;
}
Counter script
public static int Counter;
public static bool hitPlayer = false;
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
hitPlayer = true;
}
if (col.gameObject.name == "Destroyer")
{
if (!hitPlayer)
Counter++;
Destroy(this.gameObject);
}
Answer by maxoja · Sep 13, 2018 at 11:58 PM
public static int bestScore;
In the line above, you declare bestScore
as a static field, which partially means its value will persist between scene loadings. By removing the keyword static
your bestScore
will no longer keep its value after a scene is loaded.
I removed static from the code & the scores did not reset also I’m using the same scene to start the game over
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Death Counter Resets When Reloading Scene. 3 Answers
I'm trying to set a high score but I can't display it in another scene? 2 Answers
Need Help to Reset a Scene 1 Answer