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.