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 nursedayuksel · Jun 03, 2021 at 07:45 AM · scripting problemscript.playerprefs

Script with PlayerPrefs is not working on a different computer

Good day all,

I have a project I'm working on and I'm saving the highscore using PlayerPrefs. Everything works fine on my laptop but recently, I bought a new computer so I've decided to transfer the project there. However, the script that saves and displays the highscore doesn't work at all. I'm not sure why this is happening.

This is my script:

 public class Timer : MonoBehaviour
 {
     public Text timerText;
     public Text highscore;
 
     public GameObject gameOverMenu;
     public GameObject gameCompleteMenu;
 
     private float startTime;
 
     // Start is called before the first frame update
     void Start()
     {
         startTime = Time.time;
         highscore.text = "Highscore: " + PlayerPrefs.GetFloat("HighScore", 0).ToString();
     }
 
     // Update is called once per frame
     void Update()
     {
         InputDevice device = InputDevices.GetDeviceAtXRNode(inputSource);
 
         float t = Time.time - startTime;
         string minutes = ((int)t / 60).ToString("00.##");
         string seconds = (t % 60).ToString("00");
         timerText.text = minutes + ":" + seconds;
 
         if (device.IsPressed(InputHelpers.Button.MenuButton, out bool isPressed) && isPressed)
         {
             // setting time scale to 0 so that everything in the scene stops when ESC is pressed
             if (Time.timeScale == 1)
             {
                 Time.timeScale = 0;
             }
 
             // Debug.Log("You have quit the game");
             GameFinished(); // calling the GameFinished method in order to display the high score
         }
     }
 
     void GameFinished()
     {
         float t = Time.time - startTime;
         if (t > PlayerPrefs.GetFloat("HighScore", float.MaxValue))
         {
             PlayerPrefs.SetFloat("HighScore", t);
             highscore.text = "Highscore: " + t.ToString("00") + " seconds";
             PlayerPrefs.Save();
         }
     }



What could be the issue? I'm not getting any errors or anything at runtime either so I'm kinda stuck.

Comment
Add comment · Show 2
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 xxmariofer · Jun 03, 2021 at 09:47 AM 0
Share

you have not explained why it doesnt work and what happens

avatar image nursedayuksel xxmariofer · Jun 03, 2021 at 09:55 AM 0
Share

Well I wouldn't be uploading this question if I knew why it doesn't work. :) In my game there's a timer, and I save and display the highscore of this timer whenever the player ends the game (how long has the player spent their time in this level). Everything works fine on my laptop, but when I moved the project to my new computer this script just stopped working. No error, nothing. It doesn't save the highscore nor display it. It just looks "0" when I end the game.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xxmariofer · Jun 03, 2021 at 10:26 AM

your issue is in this line

      if (t > PlayerPrefs.GetFloat("HighScore", float.MaxValue))

if most likely playerprefs doesnt have any value, so it gets the default value of maxvalue, so "t" will never be higher than maxValue and "HighScore" key will never change its value

just do a comprobation

 void Awake()
 {
    if(PlayerPrefs.GetFloat("HighScore", -1) == -1)
    {
       PlayerPrefs.SetFloat("HighScore", 0);
    }
 }
Comment
Add comment · 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

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

240 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 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

How to lock an int as a playerPref? 1 Answer

The Camera is facing backwards of the Character 1 Answer

How can you change the gravity variable depending on your Scene? 2 Answers

How can i create two buttons in the inspector in editor with space between the buttons without overriding existing other controls ? 1 Answer

How to reduce my script ? 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