- Home /
How do I save and load the value of a text?
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.
Answer by Getsumi3 · Oct 30, 2019 at 08:44 AM
Hi.
1st things first. Try to avoid updating the UI and other things in Update() method. Update() is called every frame, so your code in Update() will be called every frame. You don't need to save yur progress every frame. It is enough to save it when player get some EXP (collecting stff? killing monsters etc.)
2th. In your Start() method you are trying to assign saved experience to your Slider. But what if there isn't any data saved? Thats why we need to check if the key exists and than assign saved data OR load default data.
3rd. This is only my way to do work with PlayerPrefs but, if I know that I need to use some PlayerPrefs keys I'm creating them at the Start() after checking if the keys exists.
4th. Your button method should be public so you'll be able to assign this method in inspector to your button (unless you are doing that through code, than it's ok as it is)
Here is the modified code (not tested):
public Slider barraExperiencia;
public Button botonManzana;
public Text txtNumeroNivel, txtNumeroExperiencia;
void Start()
{
if(PlayerPrefs.HasKey("Experiencia")
{
//load saved EXP
barraExperiencia.value = PlayerPrefs.GetFloat("Experiencia");
}
else
{
//assign default data;
barraExperiencia.value = 0;
}
if(PlayerPrefs.HasKey("Nivel")
{
//load saved LEVEL
nivel = PlayerPrefs.GetFloat("Nivel");
}
else
{
//load default level
nivel = 1;
}
botonManzana.onClick.AddListener(()=>ButtonAlimentoClicked());
PlayerPrefs.SetFloat("Experiencia", barraExperiencia.value);
PlayerPrefs.SetFloat("Nivel", nivel);
}
private void UpdateExp()
{
//first check
if (barraExperiencia.value >= barraExperiencia.maxValue)
{
barraExperiencia.value = 0;
nivel += 1;
txtNumeroNivel.text = nivel.ToString();
barraExperiencia.maxValue += 100;
}
//than update
UpdateUI();
}
private void UpdateUI()
{
txtNumeroExperiencia.text = barraExperiencia.value.ToString() + "/" + barraExperiencia.maxValue.ToString();
PlayerPrefs.SetFloat("Experiencia", barraExperiencia.value);
PlayerPrefs.SetFloat("Nivel", nivel);
}
void ButtonAlimentoClicked()
{
barraExperiencia.value += 10;
UpdateExp();
}
Sorry for not answering before. I have tried that code, but it doesn't work. It's true that the value of the slider is saved when the game is closed, but the txtNumeroExperiencia.text and nivel is not saved. Also, when it level ups (nivel + 1), the value of nivel is not the correct one.
Your answer
Follow this Question
Related Questions
Playerprefs to Save UI TEXT 2 Answers
Unity - keep created buttons after quit 1 Answer
Coding help: How to use xml serialization 1 Answer
how to track UI buttons on Image targets 0 Answers