Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yusufalp727 · Jul 24, 2021 at 11:43 AM · playerprefssaveadsloadadmob

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)

alt text

ekran-alıntısı.png (127.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yusufalp727 · Jul 24, 2021 at 07:25 PM 0
Share

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.

avatar image tyruji · Jul 24, 2021 at 07:55 PM 0
Share

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.

avatar image yusufalp727 · Jul 25, 2021 at 05:27 AM 0
Share

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.

avatar image yusufalp727 · Jul 25, 2021 at 05:31 AM 0
Share

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.

avatar image yusufalp727 · Jul 25, 2021 at 06:32 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Admob Interstitial loading causes game to crash?? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges