High Score doesn’t reset to 0 when loading first level form the main menu

Updated on January 9, 2019 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on January 9, 2019

Hi guys, I have a problem. So basically I’ve created a high score for my game using PlayerPrefs class.

 

 

 

It works fine on the other levels when passing from 1 to 2 and so on, but the high score shows some value when I launch the level from the main menu and I have no idea where does it take this value. 

 

 

 

I call the function to reset high score but seems like it doesn’t work at in the Main Menu as I also receive the NullReferenceException Error. Any ideas how to fix it? Here are a few screenshots from the game and script for the score for load level where it works perfectly and for Main Menu: https://photos.app.goo.gl/hGay7xhYEEw4w9CL8

 

 

 

Score script:

 

 

 

 
 
 
using UnityEngine;
using UnityEngine.UI;
 
 
 
public class Score : MonoBehaviour 
{
// creating elements for the score that shown on the player's screen
public Transform player;
 
 
 
public Text score;
public Text highScore;
 
 
 
private void Start()
{
highScore.text = PlayerPrefs.GetFloat("HighScore", 0).ToString("0"); 
}
 
 
 
private void Update()
{ 
score.text = player.position.z.ToString("0");
 
 
 
if(player.position.z > PlayerPrefs.GetFloat("HighScore", 0))
{
PlayerPrefs.SetFloat("HighScore", player.position.z);
highScore.text = player.position.z.ToString("0");
}
}
 
 
 
public void Reset()
{
PlayerPrefs.DeleteAll();
highScore.text = "0";
Debug.Log("HighScore has been reset!");
}
}
 
 
 

 

 

 

Script for loading next level, not from the Main Menu 

 

 

 

 
 
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
 
 
public class LevelComplete : MonoBehaviour
{
public void LoadNextLevel()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
FindObjectOfType<Score>().Reset();
} 
}
 
 
 

 

 

 

Main Menu Script 

 

 

 

 
 
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
 
 
public class MainMenu : MonoBehaviour 
{
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
FindObjectOfType<Score>().Reset();
}
 
 
 
public void QuitGame()
{
Debug.Log("QUIT!");
Application.Quit();
}
}
 
 
 
  • Liked by
Reply
7 on January 9, 2019

You’re never specifying what type of object you’re looking for.

It should be:

FindObjectOfType<Score>().Reset();

 

on January 9, 2019

You’re never specifying what type of object you’re looking for.

It should be:

FindObjectOfType<Score>().Reset();

 

From Mouledoux

Hm, this is weird, thanks for noticing, FindObjectOfType<Score>().Reset(); was in the scripts but for some reason, it did not appear when I created a topic for the forum. I just updated it in here so that it shows exactly how it’s in my game script. And it is not working like it supposed to work, it works perfectly fine when loading next level but not when I load level 1 from the Main Menu, any ideas what might be wrong?   

Wise
on January 9, 2019

Is there a score object in the menu scene?

If there isn’t, that would be the cause of the null ref error. Why are you deleting the PlayerPrefs on scene change? Wouldn’t you want the high score to be saved until a a very special “Clear High Score” button is pressed?

 

on January 9, 2019

There is a score object on the level itself but not on the main menu. I need a high score to be 0 when the player starts the game. Each level shows the new high score, but the idea is that it always starts with 0 on each new level. I figured out how to make high score 0 when the player starts new level after level 1 but I am having issues with setting it to a 0 when it starts from the main menu, level 1 starts and the high score already has some digits 

Wise
on January 9, 2019

There needs to be one at the main menu too,  or you need to not try and modify it at the main menu.

 

on January 9, 2019

There needs to be one at the main menu too,  or you need to not try and modify it at the main menu.

 

From Mouledoux

Understood, thank you, but I am not sure how can I put it there, as the score and high score are together with the player transform and there is no need of putting the player in the menu as it will cause more errors, score script contains player object as well, should I just copy score and high score and put it in the main menu? But that’s kind of going to be a bad style as it will work but the error about missing player object will be appearing there. I just cannot understand why at the very beginning high score already takes some value.

Wise
on January 9, 2019

You could just not reset it on the main menu

 

on January 9, 2019

You could just not reset it on the main menu

 

From Mouledoux

Thank you very much. Anyways, I figured it out, I just copied score and high score and added a few changes into the main menu script so now it works fine:

public Text score;
public Text highScore;

private void Start()
{
highScore.text = PlayerPrefs.GetFloat(“HighScore”, 0).ToString(“0”);
}

public void Reset()
{
PlayerPrefs.DeleteAll();
highScore.text = “0”;
Debug.Log(“HighScore has been reset!”);
}

public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
Reset();
}

Show more replies
  • Liked by
Reply
Cancel