Hello:
I found that the score points in my project is kept even if i restart the game or reload the current scene. the score points is kept in a static variable. here is the script:
public class ScoreManager : MonoBehaviour {
public static int score;
Text text;
void Awake()
{
text = GetComponent<Text>();
score = 0;
}
void Update () {
text.text = "Score: " + score;
}
now I want to clear that score points every time i restart my game. I tried to search a way to solve this.
at first, i found this method:
void OnLevelWasLoaded(int level)
{
if (level==0)
{
ScoreManager.score = 0;
}
}
but it looks like the unity have abandoned this function after version 5.3 (i’m using unity 5.5). Then i found the SceneManager.sceneLoaded method, i added those lines to my script:
void OnEnable()
{
SceneManager.sceneLoaded += Loadedscene;
}
void OnDisable()
{
SceneManager.sceneLoaded -= Loadedscene;
}
void Loadedscene(Scene scene,LoadSceneMode mode)
{
if (scene.name=="Start")
{
ScoreManager.score = 0;
}
}
but it didn’t work as i expected.
can anyone help me to solve this, please? thank you.