Answer the question
In order to leave comments, you need to log in
Why doesn't PlayerPrefs work?
PLayerPrefs not working
I have a system that gives +1 point for every second in the game. Game and Statistics in different scenes. If you constantly switch between scenes, and at the same time score points, then everything works fine and is saved. But if I start the test again (via "Play" from the top in the unit), then everything is reset and starts again
using UnityEngine;
using UnityEngine.UI;
public class ScoreMenu : MonoBehaviour
{
public Text sumText;
private int sumNumber;
public Text bestText;
private int bestNumber;
private void Start()
{
numPrefs = PlayerPrefs.GetInt("bestNumber");
bestPrefs = PlayerPrefs.GetInt("sumNumber");
bestNumber = Score.totalBestScore;
PlayerPrefs.SetInt("bestNumber", bestNumber);
sumNumber = Score.totalScoreNumber;
PlayerPrefs.SetInt("sumNumber", sumNumber);
PlayerPrefs.Save();
}
void Update()
{
bestText.text = "Best Score: " + bestNumber.ToString();
sumText.text = "Sum Of Scores: " + sumNumber.ToString();
}
}
Answer the question
In order to leave comments, you need to log in
So you write the value to the player prefs at the start, of course, what kind of Score class do you have, but it looks like there are zeros initially
bestNumber = Score.totalBestScore;
PlayerPrefs.SetInt("bestNumber", bestNumber);
sumNumber = Score.totalScoreNumber;
PlayerPrefs.SetInt("sumNumber", sumNumber);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question