- Home /
Question by
Yezen0Nizam · Dec 10, 2019 at 07:57 PM ·
scorescore systemscores
How to set my score to 0 when hit restart ;
I tried to do that resetting the score from playerprefmanager in the scenecontroller and in game manager but it's not working neither ways .
private void Start()
{
Scene scene = SceneManager.GetActiveScene();
Debug.Log("Active Scene is '" + scene.name + "'.");
nextSceneToLoad = SceneManager.GetActiveScene().buildIndex + 1;
highScore = PlayerPrefs.GetInt(PlayerPrefManager.HIGH_SCORE);
ResetScore();
}
private void Awake()
{
{
SetUpSingleton();
}
}
private void SetUpSingleton()
{
int numberGameSessions = FindObjectsOfType<GameSession>().Length;
if (numberGameSessions > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
private void Update()
{
if (SceneManager.GetActiveScene().buildIndex == 1)
{
ResetScore();
}
if (score > highScore)
{
highScore = score;
PlayerPrefs.SetInt(PlayerPrefManager.HIGH_SCORE, highScore);
}
Debug.Log("to achive score '" + (levelScore * nextSceneToLoad) + "'.");
if (score>=(levelScore * nextSceneToLoad))
{
LoadNextScene();
nextSceneToLoad = SceneManager.GetActiveScene().buildIndex + 1;
}
PlayerPrefs.SetInt(PlayerPrefManager.SCORE, score);
}
public int GetScore()
{
return score;
}
public void AddPoints(int amount)
{
score += amount;
}
public void LoadNextScene()
{
SceneManager.LoadScene(nextSceneToLoad);
}
void ResetGame()
{
Destroy(gameObject);
}
void ResetScore()
{
PlayerPrefs.SetInt(PlayerPrefManager.SCORE, 0);
}
}
Comment
Answer by Horothenic · Dec 11, 2019 at 01:01 AM
Your code is misleading, you are resetting to 0 every frame and then checking some conditions but at the end of the update you are setting the playerprefs to score
value.
Every frame you are setting the player prefs to 0
and then to score
.
You should have a method that triggers once per event to modify the score value, not every frame.
Thanks for the answer , it helped me reorganizing the scripts and it worked .
Your answer
![](https://koobas.hobune.stream/wayback/20220612220001im_/https://answers.unity.com/themes/thub/images/avi.jpg)