Score not resetting

Could someone please explain why my score isn’t resetting each time I restart it? It keeps displaying the previous score on load?

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour {
	public static int score = 0;

	// for the highscore
	public int highScore =0;
	string highScoreKey = "HighScore";

	void Awake(){

		DontDestroyOnLoad (gameObject);

		}


	// start this for the highscore
	void Start(){
		//Get the highScore from player prefs if it is there, 0 otherwise.
		highScore = PlayerPrefs.GetInt(highScoreKey,0);   
	}
	
	void Update(){
		guiText.text = "Score:" + score.ToString();	
	}

	void OnDisable(){
		
		//If our scoree is greter than highscore, set new higscore and save.
		if(score>highScore){
			PlayerPrefs.SetInt(highScoreKey, score);
			PlayerPrefs.Save();
		}
	}

}

While everything that Nosekills said is true, and you should definitely fully understand the static keyword before using it, there are a coulee solutions to your problem.

  1. You could use a SingletonMonobehaviour.
  2. You could reset the score on scene load.

Just set the score to zero(0) before you destroy your player and restart the game…easy as that…
Also, make sure that everytime the game starts, reset the score to zero(0), Like in Start() or Awake() function, that way… even if your game crashes abruptly, you don’t have to worry that the score won’t be reset.
:slight_smile:
if this helps, mark the question as solved :smiley:

I am also using Static variable score, but mine works fine ,take look at my code maybe It can help you. (I am new to unity and programming).

public class ScoreManager : MonoBehaviour
{

public static float score;        // The player's score.



Text text;                      // Reference to the Text component.



void Awake ()
{
	// Set up the reference.
	text = GetComponent<Text>();

	
	// Reset the score.

	score = 0;
}


void Update ()
{
	// Set the displayed text to be the word "Score" followed by the score value.
	text.text = "Score: " + score.ToString("0");


	score = Time.timeSinceLevelLoad;

	     

}

}