0

I have a code where there is an experience bar, and below it there is a level textbox and an experience textbox. Here is the code:

    public Slider barraExperiencia;
    public Button botonManzana;
    public Text txtNumeroNivel, txtNumeroExperiencia;

    void Start()
    {
        barraExperiencia.value = PlayerPrefs.GetFloat("Experiencia");
        nivel = PlayerPrefs.GetFloat("Nivel");

        botonManzana.onClick.AddListener(ButtonAlimentoClicked);

    }

    void Update()
    {
        PlayerPrefs.SetFloat("Experiencia", barraExperiencia.value);
        PlayerPrefs.SetFloat("Nivel", nivel);
        txtNumeroExperiencia.text = barraExperiencia.value.ToString() + "/" + barraExperiencia.maxValue.ToString();

        if (barraExperiencia.value >= barraExperiencia.maxValue)
        {
            barraExperiencia.value = 0;
            nivel += 1;
            txtNumeroNivel.text = nivel.ToString();
            barraExperiencia.maxValue += 100;
        }

    }

    void ButtonAlimentoClicked()
    {
       barraExperiencia.value += 10;
    }

In the code, when the button botonManzana is pressed, it increases the value of the bar. When the value of the bar reaches its max value, it returns to 0 and the variable nivel (the level) is increased.

I want to save and load the value of the bar and "nivel", and I have done it with PlayerPrefs like it's shown in the code, but it doesn't work. If someone can help me, please.

velaskus
  • 33
  • 6

1 Answers1

0

You need to call Save() to save your changes on PlayerPrefs. See https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

It might not be called automatically if OnApplicationQuit is not called, e.g. if your application is a WebGL app. See https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html for more info.

stl
  • 63
  • 1
  • 11
  • Also don't use PlayerPrefs for game state data, it was not intended for that, it was intended for user level options (e.g. volume level) which is why it does not support arbitrary objects. – Draco18s no longer trusts SE Oct 29 '19 at 20:19