- Home /
Invalid floats in PlayerPrefs
I am attempting to save several floats to PlayerPrefs. For some reason, they all come out invalid DWORD Value. The ints and string save perfectly fine. What am I missing?
Here is some code:
public class CharacterGenerator : MonoBehaviour { private float _alignment;
void GenerateStats(){ _alignment=0f; ... } void OnGUI(){ if(GUI.Button (new Rect (392, 300, 100, 50), "Start Game")){
Debug.Log("Start Our Game!"); ... PlayerPrefs.SetFloat("Alignment:", _alignment); ... } Thanks! Please forgive my noobness.
Whatever the problem is, I'm afraid it's not obvious to me. You might try stepping through your code with $$anonymous$$onoDevelop's built-in debugger, with a keen eye on watching the value of _alignment
around the time of the error.
Also, remember that you can format your code when editing (select it in the panel, and hit the "101010" button), or use a PasteBin site to post it in a friendly, out-of-the-way format.
Here is a new wrinkle: I decided to try retreiving one value from player prefs and placed into a debug log statement. It came out without a problem! Anyone have any clues???
Answer by yoyo · Mar 08, 2013 at 06:02 PM
The Windows Registry is capable of storing binary data, using the RegSetValueEx function. It appears that Unity is setting a 64-bit binary value into a REG_DWORD key when storing floating point numbers. Regedit can't deal with this, and shows "invalid DWORD", but the data is still there and can be retrieved by Unity (using RegQueryValueEx).
You can see that the data is there in Regedit with right-click > Modify.
So everything's fine, it just looks busted in Regedit. Microsoft problem.
Answer by Kryptos · Apr 21, 2012 at 12:29 PM
Could this be related to the ":" in the name of the saved prefs?
I wouldn't think so, since I used a colon on each and only the floats came back as invalid.
That stood out to me as well. I personally like to namespace my preferences using ID style na$$anonymous$$g. player1.character.alignment
Why woul dit be fine for ints and strings and not fine for floats?
Answer by Bunny83 · Apr 23, 2012 at 04:42 PM
You said that you have problems reading the value back. In your code you're just writing it. Are you sure that you use GetFloat? By looking at the error i guess you use GetInt() instead...
copy & paste error?
The problem seems to be in writing the float for some reason. GETting the float is fine, so the value is still there and saved, but in reeditm it shows up as an invalid DWORD. I have informed unity.