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 Nercoe · Oct 28, 2012 at 08:57 PM · playerdifferentworkmachineprefs

PlayerPrefs do not work on other machines, unless I recompile.

Hey guys, my project is coming to an end now and during bug testing I found a little error when testing on different machines. First of all, when I run it on my personal machine and I finish my level, it displays a working high score system that displays my highest score (Please note even if I shut my machine down and wait a week, this will still be saved). The problem is, when testing on my laptop, the high scores do not record :O!!! I can fix it by recompiling the script (to be exact I chance > to < and then back to > and click save and it works. I can only presume it is creating a file. Has anyone encountered this at all? Here are my two scripts that I used for the system:

NUMBER 1 :

 var myStyle : GUIStyle;        // GUI style for the static variable below
 
 static var myTimer : float;    // Static variable that displays a timer. This is used in multiple scripts
 
 
 // Sets the timer to 0 on startup.
 function Start()
     {
 myTimer = 0;
 }
 
 // The timer will increase by 1 every second, providing targets are still active.
 function Update () {
      if(CollisionChicken.chickenHit>=1)
      {
          //Debug.Log ("We got inside!");
           myTimer += Time.deltaTime;
  }
 
 // If the targets are destroyed, load the end game screen. Additionally, write to the string "fastestTime" so we can record a highscore. PLEASE NOTE: THIS MAY NEED TO BE RECOMPILED IN ORDER TO
 // WORK CORRECTLY. IF SO, CHANGE THE < TO > AND THEN BACK AND IT WILL WORK ONCE RE-COMPILED.
 
     if (CollisionChicken.chickenHit<=0) {
         if (myTimer < PlayerPrefs.GetInt("fastestTime")){
         PlayerPrefs.SetInt("fastestTime", myTimer);
     }
         Application.LoadLevel(2);
         Hull.hull = 100;
         Fuel.fuel = 100;
         CollisionChicken.chickenHit = 3;
         
     }
 }
 
 
 // Prints the timer variable to 1 decimal place.
 
 function OnGUI()
     {
      GUI.Label( Rect(250, 10, 100, 20), myTimer.ToString("f0"), myStyle ); 
 }

// this is where the data is recorded. SCRIPT NAME : countdown.js

NUMBER 2:

 var hScore: TextMesh; //Declares a text mesh
 
 // Print the saved data from the Countdown script after the string "High Score: ". After this has been printed, add " Seconds".
 // High Score: 10 Seconds
 function Awake(){
 
 hScore.text = "High Score: " + PlayerPrefs.GetInt("fastestTime") + " Seconds";
 }

//Displays highscore on gameover screen. SCRIPT NAME : highscoredisplay.js

As mentioned above this works fine until I use another machine. To get it working on another machine I need to recompile the script. Has anyone bumped into this before? Any help is appreciated, this has baffled me :(

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by McSteed · May 11, 2014 at 02:22 AM

I have the same issue. I've been doing most of my dev on my Surface (Windows 8) and when I bring it onto my main box (Windows 7) it doesn't work.

Here's Something I found. After deleting all prefs. My fastest time Pref was 0.0 so when I check for the fastest time there is no way for the new time to be faster. It stands to reason that a new computer would also have a a fast time of 0.0f. I added an if statement that makes a really high time if the last time was 0.0f.

Hope that helps.

 void OnTriggerEnter2D()
     {
         if (LastRaceTime == 0.0f)
             LastRaceTime = 10000f;
 
         if (raceTime <= LastRaceTime) {
 
             PlayerPrefs.SetFloat (TimeString, raceTime);
             faster = true;
         }
 
         finished = true;
     
     }
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
avatar image
0

Answer by SeeSharp · May 11, 2014 at 02:22 AM

Hi,

You are correct. The PlayerPrefs are being stored on your computer (according to the documentation).

If you want to save it, you could use an alternative like Dropbox to store it over the network. You could probably find some tutorials about that online.

Best of luck,

SeeSharp.

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
avatar image
0

Answer by Bunny83 · May 11, 2014 at 02:58 AM

You simply have a logic error here. In this line:

     if (myTimer < PlayerPrefs.GetInt("fastestTime")){

You check if your timer-values is smaller than the stored fastestTime, however if you run this code the very first time there is no "fastestTime" so the returned value is "0". Since the playtime will most likely be larger than 0 the new time will never be saved.

You can use PlayerPrefs.HasKey to check if there is actually a fastestTime stored, or simply provide a large default value when reading the value:

     // solution 1: HasKey
     if ((myTimer < PlayerPrefs.GetInt("fastestTime")) or !PlayerPrefs.HasKey("fastestTime")){
     
     // solution 2: huge default value
     if (myTimer < PlayerPrefs.GetInt("fastestTime",9999999)){

The first solution will store the new time when it's smaller than the old one or when there is no old time yet. The second solution simply passes a huge default value to GetInt which is returned when the key can't be found which is the case when you run your game for the very first time.

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

11 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

Related Questions

Where does unity save player preds when testing in the editor? 2 Answers

Player Prefs question... 1 Answer

Where is PlayerPrefs Located on Windows? 2 Answers

Multiple Save (Saving & Loading System) With PlayerPrefs 2 Answers

Spawn different Player models 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