x


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.

more ▼

asked Apr 19 '12 at 08:35 PM

Gilead7 gravatar image

Gilead7
117 18 32 39

Whatever the problem is, I'm afraid it's not obvious to me. You might try stepping through your code with MonoDevelop'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.

Apr 19 '12 at 08:42 PM rutter

Thank you!

Apr 19 '12 at 09:07 PM Gilead7

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???

Apr 19 '12 at 09:37 PM Gilead7
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Mar 08 at 06:02 PM

yoyo gravatar image

yoyo
6.4k 25 39 84

Thanks so much!

Mar 08 at 07:43 PM Gilead7
(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Apr 23 '12 at 04:42 PM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

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.

Apr 24 '12 at 02:56 PM Gilead7
(comments are locked)
10|3000 characters needed characters left

Could this be related to the ":" in the name of the saved prefs?

more ▼

answered Apr 21 '12 at 12:29 PM

Kryptos gravatar image

Kryptos
7.2k 5 32

I wouldn't think so, since I used a colon on each and only the floats came back as invalid.

Apr 23 '12 at 03:55 PM Gilead7

That stood out to me as well. I personally like to namespace my preferences using ID style naming. player1.character.alignment

Apr 23 '12 at 04:03 PM numberkruncher

Why woul dit be fine for ints and strings and not fine for floats?

Apr 24 '12 at 02:57 PM Gilead7
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4147
x294
x133
x7

asked: Apr 19 '12 at 08:35 PM

Seen: 643 times

Last Updated: Mar 08 at 07:43 PM