Player prefs not saving

I can’t get PlayerPrefs to save anything and when I go and look in the directory where they are supposed to be saved the folder isn’t even there

~/Library/Preferences/Unity/WebPlayerPrefs

Are you supposed to somehow make this folder yourself before this will work or is there some other trick to getting PlayerPrefs to save?

my code is pretty simple just as a test, straight from the help

PlayerPrefs.SetString(“Player Name”, “Foobar”);

Well weirdly now I go and look in the preferences folder and the pref file has appeared. It appears to have not been created til reboot… more investigation required.

Ok so I still can’t get this to consistently work.
In the attached is a one line script that just sets a player pref. When I run it nothing happens, nothing is written to disk.
Is this suppose to work?

100665–3893–$playerprefs_812.zip (500 KB)

ok I got the offline version working so maybe there is something else going on with the online one. I guess you can’t test it unless its on a real server.

Ok so I tested this online now off a real server and nope the preferences file doesn’t get written. I sent a bug report in so hopefully I’ll here something from Unity support.
I’m surprised that no one else here has hit this issue though I would have thought writing preferences was fairly basic stuff.

Have you tried PlayerPrefs.GetString to see if you can retrieve the string? Honestly I’ve never looked in the WebPlayerPrefs folder, but I haven’t had any trouble using PlayerPrefs both locally and hosted…

That another issue, when I used GetString and the pref file hasn’t been written the function crashes.

I wondering if it has anything to do with the player being in a div?

The whole pref thing is working fine now offline but online zip.

monark, have you seen this thread I created a little while ago?
IE7 not saving webplayer PlayerPrefs for some people?
http://forum.unity3d.com/viewtopic.php?t=14964

Does the test app I linked to save for you?

What OS browser are you using? Does it still happen if you change OS browsers?

HTML context has no effect on PlayerPrefs. Could you post the script causing the crash?

This is the section causing the issue when reading. I try and test if GetString returns anything, but even so when the file doesn’t exist the 2 globals at the bottom don’t get called, so until I can prove otherwise I’m assuming that GetString isn’t happy.

if ( !Application.isEditor ) {
		var user : String = System.Environment.UserName;
		var xmlText : String = PlayerPrefs.GetString(user);
		Debug.Log(xmlText);
		if ( xmlText != "") {
			globals.bookmarks.read( xmlText, globals);
		}
	}
		
	globals.mouseClickSpeed = 0.15;
	
	globals.bookmarksReady = true;

@Proton - Writing the pref file fails on Mac, and Pc, in safari, Firefox and IE.
I can only get it to work offline, online no platform works.

I’ll try your post and report back…

I wonder if

System.Environment.UserName;

doesn’t work online.

How do you get the user name in a web app, only by getting input from the user?

That test works fine for me everywhere. Could you post the code so I can see if it does anything different. And maybe try it on our server. The only thing I can think of is something server side blocking the writing of the prefs. Maybe some server setting? I remember we had to tweak something so it would allow unity files, perhaps its related to that.

Brilliant! it now works. It was that damn

System.Environment.UserName;

Is the only way to have unique user prefs to ask for the user name and have them enter it?

Weirdly in my little test app, I just wrote

PlayerPrefs.SetString("player", "foo");

and that didn’t work either, but back in my real app if I just use “player” as the user it works fine… go figure.

System.Environment is not accessible from a webplayer as part of the security package. Try using it again and have a look in the unity player log to see the message stating that it has been ignored since it is not allowed.