S
S
Semyon Bazhanov2020-06-23 19:56:23
Unity
Semyon Bazhanov, 2020-06-23 19:56:23

How to save numbers in unity?

Hello, I have a question about saving bonuses?
Here's how about saves

public void info2()
    {
        sv.score = score;
        sv.scoreIncrease = scoreIncrease;
        sv.tart = scoreIncrease;
        sv.levelOfItem = new int[shopItems.Count];
        sv.bonusCounter = new int[shopItems.Count];
        for (int i = 0; i < shopItems.Count; i++)
        {
            sv.levelOfItem[i] = shopItems[i].levelOfItem;
            sv.bonusCounter[i] = shopItems[i].bonusCounter;
        }
        sv.date[0] = DateTime.Now.Year; sv.date[1] = DateTime.Now.Month; sv.date[2] = DateTime.Now.Day; sv.date[3] = DateTime.Now.Hour; sv.date[4] = DateTime.Now.Minute; sv.date[5] = DateTime.Now.Second;
        PlayerPrefs.SetString("SV", JsonUtility.ToJson(sv));
    }

And here's how it works when it starts
int totalBonusPS = 0;
            sv = JsonUtility.FromJson<Save>(PlayerPrefs.GetString("SV"));
            score = sv.score;
            tyty= sv.scoreIncrease;
            scoreIncrease = sv.scoreIncrease;

            for (int i = 0; i < shopItems.Count; i++)
            {
                print((int)Mathf.Pow(shopItems[i].bonusIncrease, shopItems[i].levelOfItem));
                shopItems[i].levelOfItem = sv.levelOfItem[i];
                shopItems[i].bonusCounter = sv.bonusCounter[i];
                if (shopItems[i].needCostMultiplier) shopItems[i].cost *= (int)Mathf.Pow(shopItems[i].costMultiplier, shopItems[i].levelOfItem);
                if (shopItems[i].bonusIncrease != 0 && shopItems[i].levelOfItem != 0)
                totalBonusPS += shopItems[i].bonusPerSec * shopItems[i].bonusCounter;
            }
            
           
            DateTime dt = new DateTime(sv.date[0], sv.date[1], sv.date[2], sv.date[3], sv.date[4], sv.date[5]);
            TimeSpan ts = DateTime.Now - dt;
            int offlineBonus = (int)ts.TotalSeconds * totalBonusPS;
            score += offlineBonus;
            score2 += offlineBonus;

And for some reason everything is well preserved except for scoreIncrease, and all the time when I run the program scoreIncrease = 0. Please tell me what to do. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zZaKko, 2020-06-24
@mamora_sema

PlayerPrefs.SetInt("Cell Name", data(int));
With this method, you store your variable in a register.
To take it back:
int x = PlayerPrefs.GetInt("Cell Name");
This way you can load your variables permanently even if the game is closed and restarted.
I think it helped)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question