This may sound like a dumb question coming from someone who’s been using Unity for 2 years now, but it’s not working no matter what I try. Here’s what I have:
The problem with the above code is it does not save. When I load the next scene, it zeroes out the PlayerPref, even though I have no code to do that in the next scene or in the transition between scenes. I don’t even have the bestDistance variable in the next scene yet.
When printing the PlayerPref from the time I change it to the time I load the next scene, it displays the correct value upon changing, but going to another scene zeroes it out. However, if I quit the game before going to the next scene, it does save. So changing scenes prevents the PlayerPrefs from saving.
If needed, I could post my full script.
Using Unity 5.5 / Windows 8.1 (am stuck with this filth for now)
My only guess is something is happening that is overwriting the playerpref. Or, you may be trying to read in the playerpref, but maybe you have a typo, which means it’s returning a default value ( this is a thought).
So, you may want to verify that your are getting the proper playerpref. Otherwise, you may need to show some more code for where you are dealing with your playerprefs.
Are you possibly switching scenes to fast that the playerpref never gets set?
Could test this with a print statement in your PlayerIsDead function and see if it prints out before a scene switch. You say it works when you don’t switch scenes which makes me wonder if this is the case.
Unless it takes more than about 10 seconds to save it. When the player dies, I have an animation that lasts about 10 seconds, so the PlayerPref is set about 10 seconds before the scene even switches.
EDIT: I forgot to mention that when I tried the same code to do this that I had in an older project that worked (worked in an older version of Unity, 5.something), so if something’s changed in the 5.5 update regarding PlayerPrefs, that might be the cause. But I haven’t seen anything about any PlayerPref changes.
I’m using 5.5.1 on a project at work that uses playerprefs and have no issues.
Now, one thing to be mindful of, and I’m not sure if this could create a problem is that you are doing this in update. Which means, since you say you have an animation playing, that you PlayerPrefs is getting called several times a second. Not usually an efficient way of doing it. Generally I would suggest that when you player dies, you set the playerpref once at that point. Now, I can’t say if this would create problems or not, but it’s something to note.
Otherwise, I’m not seeing any issues with the code you posted.
Iv used playerprefs many times how ever i always format my names for them like so “Player_Health” “Player_ID” etc etc. So there could be a chance that having a space “Player Health” may be effecting it? not sure just a guess.
Hey @ChrisIsAwesome_1 . I had similar problem in the past. I can’t remember what exactly was the issue but one thing that I would advise is take your PlayerPrefs code related code and try it on isolated project to make sure your logic is fine (syntax looks ok).
Speaking of which there are couple of things you could check.
function PlayerIsDead ()
{
if (currentDistance >= bestDistance) {
Debug.Log("Storing best distance"); // <---- Log something out to see if you actually saving
PlayerPrefs.SetFloat ("Best Distance", bestDistance);
PlayerPrefs.Save(); // <---------- This seems to be missing in your code
}
}
Forgot to put the line PlayerPrefs.Save () in the code I posted. I didn’t copy/paste code because wanted to leave out unnecessary bits of code. Just forgot to write in that line.
Anyways, I’ve tried doing that. My code is:
function PlayerIsDead ()
{
if (currentDistance >= bestDistance)
{
bestDistance = currentDistance;
print ("Current distance is: " + currentDistance);
print ("Best distance is: " + bestDistance);
PlayerPrefs.SetFloat ("Best Distance", bestDistance);
PlayerPrefs.Save ();
print ("PlayerPRefs has been saved as: " + PlayerPrefs.GetFloat ("Best Distance");
}
}
I also now call is PlayerIsDead function from the player script at the start of the death animation (so it is only called once, rather than calling it in Update). The output in the console is:
Current distance is: 1.4
Best distance is: 1.4
PlayerPrefs has been saved as: 1.4
However, in the Awake function in the script in the next scene, I have print (PlayerPrefs.GetFloat (“Best Distance”); and that returns 0.
Problem was in another script that controlled global input (input constant across all scenes for debugging purposes), I had the line:
if (Input.GetKey (KeyCode.Delete))
{
PlayerPrefs.DeleteAll ();
}
And apparently, even though the key wasn’t being pressed, Unity was acting like it was upon loading scenes. Might be my keyboard actually because the Delete key is a function key. But problem solved! Thank you all for your help!