The issue was with the editor itself, deleting player prefs file fixed the issue.
PlayerPrefs defaulting to 0
Hi Guys, I can't seem to fix this simple problem. When testing in editor or running a build for the first time, the music volume and SFX volume keeps defaulting to 0. This is my code. Am I doing something wrong?
function Start() {
//Set SFX and Music Sliders to correct positions.
if(!PlayerPrefs.HasKey("musicVolume")) PlayerPrefs.SetFloat("musicVolume",1);
if(!PlayerPrefs.HasKey("sfxVolume")) PlayerPrefs.SetFloat("sfxVolume",1);
var setMusicVolume = GameObject.Find("MusicSlider").GetComponent(UnityEngine.UI.Slider);
setMusicVolume.value = PlayerPrefs.GetFloat("musicVolume");
var setSfxVolume = GameObject.Find("SfxSlider").GetComponent(UnityEngine.UI.Slider);
setSfxVolume.value = PlayerPrefs.GetFloat("sfxVolume");
}
Answer by newtquestgames · Nov 07, 2015 at 08:57 PM
I had a problem like this because the slider's On Value Changed was set to "Editor and Runtime".
What happened was this; after playing the scene the editor would display the slider in the scene window with a value of 0. This triggered my On Value changed handler (nb: Editor and Runtime) which would then save the editor's 0 value for the slider into the player prefs.
Fix: Set the slider's On Value Change to Runtime Only.
I discovered this by using logging. The value 0 was always saved to player prefs when I stopped running the game to return to the editor.
newtquestgames I think you might be on to something. Thanks, I'll check it out, this could be the problem.
Answer by Dinosaurs · Oct 25, 2015 at 04:16 PM
It might be that you're passing an integer; try passing 1f or 1.0 to SetFloat.
No. It's not passing an integer. The integer is converted to a float at compile time. The executing code never sees 1 as an integer, but as a float.
Consider the following example:
public sealed class Example : $$anonymous$$onoBehaviour
{
void Start()
{
printFloat(1.0f); // Note 1.0 would be an error because that's a double.
printFloat(1f);
printFloat(1);
}
void printFloat(float value)
{
print(value);
}
}
It calls printFloat, which accepts a float value. I am calling it three times, once per variation discussed (1.0f, 1f, 1). Inspecting the compiled assembly in ILspy, we can see what the compiler did:
public sealed class Example : $$anonymous$$onoBehaviour
{
private void Start()
{
this.printFloat(1f);
this.printFloat(1f);
this.printFloat(1f);
}
private void printFloat(float value)
{
$$anonymous$$onoBehaviour.print(value);
}
}
The reverse compilation of the IL instructions to C# showed that it compiled the equivalent of using 1f for all three cases. We can also look deeper into the IL instructions for Start only to sanity check what actually goes on.
.method private hidebysig
instance void Start() cil managed
{
// $$anonymous$$ethod begins at RVA 0x23e0
// Code size 34 (0x22)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.r4 1
IL_0006: call instance void Statement.Example::printFloat(float32)
IL_000b: ldarg.0
IL_000c: ldc.r4 1
IL_0011: call instance void Statement.Example::printFloat(float32)
IL_0016: ldarg.0
IL_0017: ldc.r4 1
IL_001c: call instance void Statement.Example::printFloat(float32)
IL_0021: ret
} // end of method Example::Start
Before each call to printFloat, the implicit "this" reference and a constant real4 (that is a float - real number, 4 bytes long) is loaded on the stack and the method is executed. It'll make no difference what so ever adding a floating point suffix - the compiler can infer that from usage (the method has a float parameter).
(If you are eagle eyed, you see the method called is "Statement.Example::printFloat". I put all code in a namespace called "Statement" but excluded that from the example code to preserve line width. Functionally, it makes no difference.)
Answer by Statement · Oct 25, 2015 at 04:32 PM
if(!PlayerPrefs.HasKey("sfxVolume")) PlayerPrefs.SetFloat("sfxVolume",1);
This will only set the float to 1 if the key does not exist. If the key exists (if you set it to 0 some time ago for example), it will do nothing.
setSfxVolume.value = PlayerPrefs.GetFloat("sfxVolume");
This will get the value, which was previously stored as 0.
You can clear playerprefs with DeleteKey or DeleteAll.
A nicer way to specify a default value, is to provide one when calling GetFloat.
setSfxVolume.value = PlayerPrefs.GetFloat("sfxVolume", 1);
Note that it'll still return 0 until you delete the previous value you stored there.
Answer by LexGear · Dec 10, 2015 at 03:28 AM
So it turns out the problem was with the editor. Finding and deleting the player prefs file fixed this issue for me.