saving floats in playerprefs

Hi,

I have a slider bar and trying to save that info from the slider bar (float). I use

PlayerPrefs.SetFloat(“brightness_setting”, brightness_setting);

to save the float brightness_setting and I use

brightness_setting = PlayerPrefs.GetFloat(“brightness_setting”, 0.5f);

to load it. When loaded I assign brightness_setting to the slider bar below
gammaslider.value = brightness_setting;

but it doesnt work. I noticed in the registry its not saving the value properly as well, says “invalid DWORD (32bit) value”

anyone know the fix for this?

Thanks

Are you getting any errors in console or is it just not loading the value correctly?

Can you share your code?

I get no errors in the console and theres not much else code wise to share. The variable brightness_setting shows its being changed in the inspector but just doesnt seem to save the value properly

this
brightness_setting = PlayerPrefs.GetFloat(“brightness_setting”, 0.5f);
is wrong
brightness_setting = PlayerPrefs.GetFloat(“brightness_setting”);
is what you need :slight_smile:

0.5f is just the default if a value dont exist? but I tried without anyway and same issue.

Did you ever store something besides a float in that “brightness_setting” field, like maybe an int? Try doing PlayerPrefs.DeleteKey( “brightness_setting”); and then write it again with PlayerPrefs.SetFloat()…

You could also try Debug.Log(PlayerPrefs.HasKey(“brightness_setting”)); and see if that returns true?

or try calling PlayerPrefs.Save(); after your SetFloat if it returns false?

I still don’t know exactly what your code looks like so I can’t be sure what’s going on.

Its highly vital to call Save() hehe

Usually not; PlayerPrefs is saved automatically when your application exits (unless there’s a crash or other uncontrolled exit).

Definitely do NOT call PlayerPrefs.Save() all the time, but it’s a great type of thing to call when you do certain key data change steps:

  • attain a new high score to the chart
  • change settings (save it when you close the settings window)
  • after a bunch of user info is requested and stored to PlayerPrefs.

Those are some examples of great times to call .Save(). I also call save whenever I get back to the main menu, generally speaking.

It’s rare to lose stuff because of failure to call .Save() but it happens.

As requested, show your code, in particular where you declare the variable brightness_setting. Also, try hard coding a float value instead of using the variable.