Reset Score

Good Morning!
I’m trying to do a simple thing, catch a text element from my UI and store the total points of the player.

So i created a script called Score:

    public Text scoreText;   
    private int playerScore = 0;
    private int totalScore = 0;
   
     void Start()
    {          
        scoreText.GetComponent<Text>();       
    }    
    void Update()
    {
        totalScore = playerScore + Feed.hit;
        scoreText.text = totalScore.ToString("0");        
    }

The Feed.hit is from the script Feed, this add a punctuation when an object hit another:

    public static int hit = 0;   
    private void OnTriggerEnter(Collider other)
    {       
        if(other.gameObject.tag == "Doe")
        {
            hit+=5;
            Destroy(other.gameObject);
        }
        if(other.gameObject.tag == "Stag")
        {
            hit+=10;
            Destroy(other.gameObject);
        }       
        if(other.gameObject.tag == "Moose")
        {
            hit+=15;
            Destroy(other.gameObject);
        }       
    }

On my GameManager i call the Restart if the game is over and on my method “RestartGame()”
i also do the reset of score:

    public GameObject gameOver;
    public static GameManager instance;
    public static bool GameIsPause = false;
    private Score scoreManager;
   
    void Start()
    {
        instance = this;
        scoreManager = FindObjectOfType<Score>();
    }
    public void ShowGameOver()
    {
        gameOver.SetActive(true);
        Time.timeScale = 0f;
        GameIsPause = true;
    }

    public void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        Time.timeScale = 1f;
        GameIsPause = false;
        scoreManager.scoreText.text = "0";                  
    }

But it never reset my score.

I tried to do this on my score too:

    public Text scoreText;   
    private int playerScore = 0;
    private int totalScore = 0;
   
    void Start()
    {          
        scoreText.text = PlayerPrefs.GetInt("TotalScore", 0).ToString();      
    }
    void Update()
    {
        totalScore = playerScore + Feed.hit;
        scoreText.text = totalScore.ToString("0");
        PlayerPrefs.SetInt("TotalScore", totalScore);
    }
    
    public void DeleteScore()
    {
        PlayerPrefs.DeleteAll();
        totalScore = Feed.hit * 0;
        scoreText.text = totalScore.ToString("0");
    }

Even when starting the game set the score to 0, but nothing.

Can someone help me?

You’re forgetting to reset Feed.hit. Since it’s a static variable it doesn’t get reset when the Feed object is destroyed and recreated. Static variables are not tied to any specific object.

Is there a good reason why you used a static variable in the first place?

1 Like

To access my Trigger from Feed and know how much points i get.
I think about that actually, but i don’t know how to reset it.
You see on the last script on line 20? I tried to multiply to 0. I know is not a good thing, but just for a test.

Wouldn’t this work?

Feed.hit = 0;
1 Like

Maaaannn… I feel me so dumb sometimes, I’m place it on the reset button and work, thanks a lot!