Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

saving floats in playerprefs

Discussion in 'Scripting' started by 2dgamemania, Oct 11, 2018.

  1. 2dgamemania

    2dgamemania

    Joined:
    Apr 30, 2014
    Posts:
    153
    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
     
  2. liphttam1

    liphttam1

    Joined:
    Nov 11, 2014
    Posts:
    9
    Are you getting any errors in console or is it just not loading the value correctly?

    Can you share your code?
     
  3. 2dgamemania

    2dgamemania

    Joined:
    Apr 30, 2014
    Posts:
    153
    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
     
  4. Barry100

    Barry100

    Joined:
    Nov 12, 2014
    Posts:
    200
    this
    brightness_setting = PlayerPrefs.GetFloat("brightness_setting", 0.5f);
    is wrong
    brightness_setting = PlayerPrefs.GetFloat("brightness_setting");
    is what you need :)
     
  5. 2dgamemania

    2dgamemania

    Joined:
    Apr 30, 2014
    Posts:
    153
    0.5f is just the default if a value dont exist? but I tried without anyway and same issue.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    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()...
     
  7. liphttam1

    liphttam1

    Joined:
    Nov 11, 2014
    Posts:
    9
    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.
     
  8. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Its highly vital to call Save() hehe
     
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Usually not; PlayerPrefs is saved automatically when your application exits (unless there's a crash or other uncontrolled exit).
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    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.
     
  11. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    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.