D
D
Denchik1020302021-11-12 20:36:39
Unity
Denchik102030, 2021-11-12 20:36:39

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

1 answer(s)
P
pashamaladec, 2021-11-12
@pashamaladec

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);

And you also get numPrefs and bestPrefs, which you don’t use anywhere

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question