As simple as calling the Playerprefs.SetFloat(value).
In your Start() you would put the PlayerPrefs.GetFloat(“Volume”). Something like this. I am not a JS programmer so I don’t know if this will work directly.
var Volume : float;
function Start()
{
if (PlayerPrefs.GetFloat("Volume") != null) {
Volume = PlayerPrefs.GetFloat("Volume");
}
}
function Update()
{
//checks to see if the value has changed before modifying it
if (AudioListener.volume != Volume) {
AudioListener.volume = Volume;
PlayerPrefs.SetFloat("Volume", Volume);
}
}
function OnGUI()
{
Volume = GUI.HorizontalSlider(Rect(506,390,100,10), Volume, 0.0, 1.0);
}
Do not use uppercase letters in your PlayerPrefs keys. I ran into issues with this when publishing to mobile devices. Other than that, @CaptainMurphy_1 is spot on.
It is probably a matter of the reference actually getting saved as a lowercase no matter what. So putting in “Volume” may get saved as “volume” so when you go to retrieve it the value would be null.
Android devices, possibly just mine in particular, have issues when using capital letters in your PlayerPrefs keys. I beat my head against the wall for a full day wondering why I was having no issues on any other platform besides Android only to find the culprit was a capital letter at the beginning of a few PlayerPrefs keys. Since then I always use lowercase letters to store PlayerPrefs and have had no issues since.
I have been working almost exclusively in desktop projects so I had no idea about this. May need to just add that to the list of things to change in our projects just for the sake of not losing time chasing the issue.
If you intend to publish to mobile at some point I would advise that you do. Seriously, a full day to figure that out; when I did finally squash the bug I felt so stupid that I missed something so small, but who would have thought that a capital letter in the PlayerPrefs key would even be a problem? You’d think it would just work. :-p
I meant what types of issues. Values not being saved at all? Values being altered in between saving and loading? The value names being case-insensitive?
Also, did you ever report this as a bug with a reproducible test case?
The issue is that even though I saved the value with a capital letter in the key it was still stored in all lowercase letters, so when I tried to get the value it would always return null. I never reported the bug, because I didn’t really consider it a huge deal once I figured out what was causing the problem. Just use lowercase letters and it’s easily avoided. It’s not any extra hassle, but if you don’t know to only use lowercase letters it can certainly cause you hours of frustration.
Fact remains, it’s a bug and should be fixed (if it hasn’t been in the meantime). Maybe it’s not a big deal for you now that you’ve figured out a workaround, but there’s no sense just fixing the symptom when you could report a bug and fix the root cause, for everybody.
(I don’t work in Android or have a device to test on, or I’d report it myself)
I do a lot of work in Android; I’ll report the bug later today and at least bring it to their attention. It may only be certain devices. I have an HP Slate 7 which I guess is a little obscure compared to a Google Nexus or Samsung Galaxy Tab.
If you are going to spend any amount of time working on something it is always better to err on the side of caution. I’ve just put the lowercase keys into my common practice. I do it without even thinking about it now because I know it’s always safe.