- Home /
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 :(
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;
}
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.
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.
Your answer
Follow this Question
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