Unable to Reset the game "Score" to zero when pressing on "Play Again" button

I am practicing to make 2D Laser Defender, When I try to use ResetScore() method from ScoreKeeper script in Level Manager script at Load Game() method it throws a nullreferenceexception.

Level Manager Script:

Score Keeper Script:

Error Message on Unity:

Please look in to the issue and advice.

First, you are returning the score from ResetScore() but it doesn’t seem to be necessary, so you can change that to a void

public void ResetScore()
{
    score = 0;
}

Then, you made ScoreKeeper a singleton so there is no reason to FindObjectOfType in Awake(). You can access the singleton directly

public void LoadGame()
{
    ScoreKeeper.instance.ResetScore();
    SceneManager.LoadScene("Game");
}
3 Likes

Thank you so much.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms