Answer the question
In order to leave comments, you need to log in
How to work with PlayerPrefs?
Hello, I'm making a game and I ran into a problem that I need to save coins :) I found out that there is PlayerPrefs and even understood how to use it (if I may say so) the script works but it doesn't save more than 1 :(
this is the save code
public Text text;
public int Score = 0; // вот эта переменная
private void OnApplicationQuit()
{
PlayerPrefs.SetInt("Key1", Score);
}
public void Update()
{
PlayerPrefs.GetInt("Key1");
}
private void OnTriggerEnter2D(Collider2D collision)
{
Score++;
PlayerPrefs.SetInt("Key1", Score);
Destroy(gameObject);
}
public Text text;
public int Score;
private void Start()
{
Score = PlayerPrefs.GetInt("Key1");
}
private void Update()
{
Score = PlayerPrefs.GetInt("Key1");
text.text = "$ " + Score ;
}
Answer the question
In order to leave comments, you need to log in
Preservation -
private void OnApplicationQuit()
{
PlayerPrefs.SetInt("Key1", Score);
}
private void Awake()
{
Score = PlayerPrefs.GetInt("Key1", 0);
}
When exiting, if the account was 1 last time, but did not collect anything this time, then the code will assign 0 to the account. And I still don’t understand why you constantly take the value from PlayerPrefs in Update, throw the text update in OnTriggerEnter where there is score incrementation
. Everything is somehow confusing for you. Your object is destroyed and if it doesn't reach applicationquit, then there will be no saving. The script is destroyed along with the object. Make a separate script that is responsible for saving / loading.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question