- Home /
There are wrong mathematical equations in PlayerPrefs. How can I resolve it?
Hello! The gold earned during the game is displayed at the top of the screen. If Game Over happens, the Game Over panel will appear and the total gold earned in the game is displayed on this panel. If the player presses the 2X button, they will earn double gold after watching the ad. At the same time, 2X gold will be written on the Game Over panel because the advertisement is watched. And all the collected gold is displayed on the main menu scene. I tried to do this with playerprefs, I don't know why but wrong actions occur. For example, after watching the ad I won 5 gold in the game, the total amount of gold earned should be 10, but it is 9. In the main menu scene, 8 gold is added on top of the previous gold. How can I resolve this situation? I need your help. Thanks!
My game:
private void OnCollisionStay(Collision collision)
{
if (collision.gameObject.tag == "Gold")
{
PlayerPrefs.SetFloat("Gold", gold);
PlayerPrefs.Save();
gold ++;
goldText.text="Gold: " + gold;
Destroy(collision.gameObject);
}
}
Codes for 2X gold rewarded:
public void videoyuizlediOduluHaketti(object sender, Reward args)
{
gold= 2 * PlayerPrefs.GetFloat("Gold");
goldText2x.text="Gold: " + gold;
PlayerPrefs.SetFloat("Gold",PlayerPrefs.GetFloat("Gold") + gold);
}
To show total gold amount on Game Over Panel:
void Start()
{
gold=PlayerPrefs.GetFloat("Gold");
goldText.text="Gold: " + gold;
}
Total gold amount on menu scene:
void Start()
{
//total gold amount
PlayerPrefs.SetFloat("Gold",PlayerPrefs.GetFloat("Gold") + gold);
gold=PlayerPrefs.GetFloat("Gold");
goldText.text="Gold: " + gold;
}
Game Over Panel: after game over total gold=5 on game over panel total gold=2708: (But it should be 5. After rewarded ads it will be 10)
Answer by tyruji · Jul 24, 2021 at 02:33 PM
You are not actually multiplying your "gold" by 2, because you are adding it later on anyway which is the same as multiplying it by 3. Here:
gold = 2 * PlayerPrefs.GetFloat( "Gold" );
goldText2x.text = "Gold: " + gold;
PlayerPrefs.SetFloat( "Gold", gold ); // no need to add the prefs gold
That's how it should look like, let me know if there are still any issues.
Thank you for answering! It seems to work but I'm not sure. Sometimes errors occur in mathematical operations. sometimes the result of the previous game remains saved in the Game Over panel. Sometimes the total amount of gold in the Menu scene is reset. This way there are errors. Also, when I print new values, it sometimes collects incorrectly. What could be the reason for this? For example, our current value is 20, 5 more will be added to this value, the new value should be 25, but sometimes 1-2 numbers are missing or adding more.
I am not sure why it might be miscalculating the small parts, (perhaps you are doing something to the "Gold" value in a different script?) but the reason why your score saves from your previous game is because that's how PlayerPrefs is supposed to work - it saves data in registers of your computer and it's very suitable to save small information like int or float values.
I think if I make float values into integers, it will not round anymore. for example 1.3 + 2 = 3.3, he can round it to 3. If I use variable types as int, I think there will be no rounding.
I am using gold in four different script files. and I'm using PlayerPrefs to save the data and use it. I wonder if it will be healthier if I use the Save-Load system instead of PlayerPrefs.
also, if the total gold is equal to zero when Game Over, the total gold amount in my Menu scene will suddenly double and the previous game's score will be displayed in the Game Over panel.
Your answer
Follow this Question
Related Questions
PlayerPrefs for FPS? 1 Answer
Saving character selection 1 Answer
Save/Load Variable with playerprefs 1 Answer
Set int to object from list? 0 Answers