Resetting Score to Zero on Restart of Game

I am making A VR game, in which there is only one level which is on main scene and the other scene is of “end” on which the game Over text Score is visible with Restart(which reloads the main scene) and Exit Button.
My problem is, M using this script as my ScoreManager Script given below : I want this score in end scene too and this is working as m using PlayerPrefs
But the main problem is, when clicking the restart at the end scene, the game reloads the Main scene but that score Still have the same value of the previous game. I want it to set to Zero. Please help !!!

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

namespace CompleteProject
{
    public class ScoreManager : MonoBehaviour
    {
        public static int score ;        // The player's score.
     
        Text text;                      // Reference to the Text component.

      
        void Awake()
        {
            // Set up the reference.
            text = GetComponent<Text>();
            score = 0;
            score = PlayerPrefs.GetInt("Score");

                     
         
        }

 

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

        }

     
    }
     
}

I have also used public static void DeleteKey(int score) to delete the score, but nothing happened :frowning:

PlayerPrefs.SetInt(“Score”, 0);

Anyone? Thought of that?

thanks for your reply , i tried as you said but it makes score “o” at the end of game, its not working ):

I think,that’s because you define score as static.Remove static and check if that works.
Also

score = 0;
score = PlayerPrefs.GetInt("Score");

You pull value at the awake.
First check if end scene is loaded and if it is pull score from playerprefs.

I assume u use same script for both scenes?

// Add to score manager
public void ResetScore()
{
    score = 0;
    PlayerPrefs.SetInt("Score", score);
}

Now call that code above either when you press the reset button.

Thanks for the reply.

By removing static from the code giving error as I used ScoreManager in Enemy Health script too and it requires score to be static.

And Yes M using the same script for both the scenes.

Thanks For the reply, but this code is not working. I have already tried this :frowning:

How do you mean it is not working? Do you get any errors? It should work, unless you have some code somewhere else where you override the score again. Can you try again, but remove the SetInt line in the Update. Make sure you only save the score in the level scene when the level is completed one way or another. make a method called SetScore, and only call that on level complete, and on reset.

// Add to score manager
public void SetScore(int score)
{
    this.score = score;
    PlayerPrefs.SetInt("Score", this.score);
}

Well…because you use same script for both scenes,it calls PlayerPrefs.GetInt(“Score”) on loading both scenes.You don’t want to do that if you want start new game with score 0.Simply wrap it in

if(Application.LoadedLevelName =="your end scene name ")
{
Score = PlayerPrefs.GetInt(" Score");
}

And it will only read your value when your end scene is loaded.But because your var is static you can just remove that line from Awake and just before restart reset it to 0.

Hi… I am having the same problem… All of my scenes (Intro_Canvas, Game_Canvas, GameOver_Canvas) are located in 1 scene… I have tried:

if(Application.LoadedLevelName ==“OutsideScene”)…

and it works… Score goes back to Zero … But… I am wondering if there is any way to return the score to zero within the scene file Without going to an outside scene… Example… Is there a way to tell the score to go back to Zero only when the Game_Canvas Awakes or Starts… ? I ask this because when I use:

PlayerPrefs.SetInt(“Score”, 0);

Score = 0;

…the score goes back Zero in the GameOver_Canvas, which is no good… and also screws with my HighScore chart… Is there a way to return the score to zero within the scene file(when the Game_Canvas awakes) without effecting the GameOver _Canvas? Fbm…

@fredmiller12345 : Can you please post that in a new thread, and use code tags for code and normal text for the rest.
There is a pinned thread on code tags in the forums, if you’re not sure how to do it.
So much bold and underline is not necessary :slight_smile:

I am no expert, you could try to set the value of score to whatever the default is but just assigning the score variable the initial value when the restart button is pressed.
like:

ScoreManager.score = 0;
// inside the restart button function