Answer the question
In order to leave comments, you need to log in
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));
}
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;
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question