S
S
SKLLZ12020-06-09 19:46:52
Unity
SKLLZ1, 2020-06-09 19:46:52

How to load save once?

Faced a problem. I made a save through PlayerPrefs, in which it is saved in onapplicationquit, and loaded in awake. And this script is attached to the menu, and in the same script there are methods for increasing these values ​​​​(did a type of statistics). but here's the problem, when i exit the level back to the menu, it loads the save again...

private static int points;
private static int enemys;
private static int asteroids;
private static int shields;
private static int asteroidsLose;

private Saver sv = new Saver();

void Awake()
{

    if (PlayerPrefs.HasKey("SV"))
    {

        sv = JsonUtility.FromJson<Saver>(PlayerPrefs.GetString("SV"));

        points = sv.points;
        enemys = sv.enemys;
        asteroids = sv.asteroids;
        shields = sv.shields;
        asteroidsLose = sv.asteroidsLose;

    }
    else Debug.Log("Сохранения нет");

}


void Start()
{

}
void Update()
{

}

private void OnApplicationQuit()
{

    sv.points = points;
    sv.enemys = enemys;
    sv.asteroids = asteroids;
    sv.shields = shields;
    sv.asteroidsLose = asteroidsLose;

    PlayerPrefs.SetString("SV", JsonUtility.ToJson(sv));

}


[Serializable]
public class Saver
{
    public int points;
    public int enemys;
    public int asteroids;
    public int shields;
    public int asteroidsLose;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
2
2CHEVSKII, 2020-06-09
@2chevskii

The most obvious thing you can think of is that instead of separate variables, keep a global of the Saver class that you check for null and load if it is. If you want to shorten the value access code, make properties.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question