- Home /
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();
}
}
}
Does it not restart on scene loading or game loading?
**Hint its static
It doesn't start at 0 when restarting the game for example when the player loses all his lives and the game is restarted..?
I've made it static because another script accesses it depending on what colours been hit?
That is the problem. Remove it from static and get a reference to the script.
Do you $$anonymous$$d explaining how I can do this? I've removed static now but the script accessing is giving an error;
void OnCollisionEnter (Collision col)
{
//Debug.Log (col.gameObject.name);
Score.score += (int)blockType;
if (lives == 0) {
//audio.Play();
Destroy (gameObject);
AudioSource.PlayClipAtPoint(pickup, transform.position);
}
Think of it this way... A static variable public static int score = 0;
belongs to the class Score. A non-static variable belongs to an instance of a class (if you're confused about the difference between a class and an instance of a class, you should definitely read about the subject)
When the script "Score" you posted here is attached to a GameObject and running in your game, it is an instance of class Score. You could as well have a 100 GameObjects with the same script and each of those instances of "Score" could have their own highscore and highscore$$anonymous$$ey variables that have values that are different from all the other 100 Score instances.
This way it's possible to i.e. make a horde of zombies that each use the same Script but behave differently because their non-static variables have different values.
static variables are referenced with the class name like you did: Score.score += (int)blockType;
Instance variables are referenced through the specific instance which you want to access. For example something like:
// find the right GameObject with the name it has in the Hierarchy
GameObject scoreObjectInstance = GameObject.Find("Name Of The Game Object That Has Score Script Attached");
// get the instance of Score that's running attached to this GameObject
Score scoreScriptInstance = scoreObjectInstance.GetComponent<Score>();
scoreScriptInstance.score += (int)blockType; // change the score in this specific Score-instance
Static variables keep their value until you specifically assign them a new value, whereas non-static variables "die" when the instance of the script dies, for example when you change Scenes in your game and all GameObjects get destroyed and the scripts attached to them as well.
Answer by iwaldrop · Apr 27, 2014 at 03:03 AM
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.
You could use a SingletonMonobehaviour.
You could reset the score on scene load.
I tried the second option suggested regarding the scene load but nothing was being displayed I thought I'd test it out with a debug message!
This is the code I've used:
void OnLevelWasLoaded(int level) {
if (level == 3)
curScore = 0;
}
Remove the conditional and try again. The only reason that you'd need it is if you want to treat one level differently than the rest.
Yeah I tried that too .. I understand why I need to use it but not sure where to place it? Does it need to go before anything in particular. When carrying out my research it said to place it right at the top?
Answer by sumeetkhobare · Apr 28, 2014 at 10:58 AM
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. :) if this helps, mark the question as solved :D
Perhaps the player can be destroyed several times per scene because he has many lives, or because of some other gameplay mechanic. This is why, in general, it's a bad idea to write speghetti code, where control is decentralized and things like the score are allowed to be altered by anything. It just leads to bugs that are difficult to debug later down the road.
Answer by Ali_unity · Jul 21, 2015 at 09:26 PM
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;
}
}
Your answer
Follow this Question
Related Questions
PlayerPrefs Not Working 0 Answers
Sorting arrays? 1 Answer
HighScore Manage Using PlayerPrefs 1 Answer