- Home /
Counter doesn't reset
I have a empty gameobject counting the number of objects that collides with trigger but when the game is over & I reload the scene but the counter score remains the same
Restart Button Script
 public void reload()
 {
      SceneManager.LoadScene("Game");
 }    
Gameobject 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);
      }
Score Script
 Text score;
 public 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 = "Score " + Enemy.Counter;
  bestScoreText.text = "Best " + bestScore;
 }
No its restarting the game & playing the game over.
Answer by Hellium · Sep 24, 2018 at 09:57 PM
Because Counter is static, its value is not reset. You will have to explicitely call Counter = 0 in the Awake or Start function. 
I put Counter = 0 in the awake function & it stuck at 0 after the first time I play it
You probably create those Counter scripts at runtime all the time. So you can't set it to 0 inside the Counter script. Set it to 0 in the Score script. Just inside Start:
 void Start ()
 {
     Enemy.Counter = 0;
     score = GetComponent<Text>();
     bestScore = PlayerPrefs.GetInt("Best");
 }
Note that you have a typo inside Update. You used GetInt again where you most likely wanted to use SetInt (line 16 in your Score script here)
I put Enemy.Counter = 0 in the start function in the score script & it’s stuck at 0 I don’t know why also SetInt gave me errors when I tried adding a int value
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer
How to fix my Life counter 1 Answer
GUI.box (Score counter) doesn't appear when game is on play. 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                