Hey there
I use a Toggle Component to toggle music on/off. I want to save the PlayerPrefs of course but when I quit the Editor play mode, I can see in the debugger that the toggle function is being called which doesn’t really make sense to me.
Stack
SoundManager.toggleMuteSounds () in E:\mygame\Assets\scripts\SoundManager.cs:96
UnityEngine.Events.InvokableCall.Invoke (args={object[1]}) in C:\buildslave\unity\build\Runtime\Export\UnityEvent.cs:153
UnityEngine.Events.InvokableCallList.Invoke (parameters={object[1]}) in C:\buildslave\unity\build\Runtime\Export\UnityEvent.cs:634
UnityEngine.Events.UnityEventBase.Invoke (parameters={object[1]}) in C:\buildslave\unity\build\Runtime\Export\UnityEvent.cs:769
UnityEngine.Events.UnityEvent.Invoke (arg0=true) in C:\buildslave\unity\build\Runtime\Export\UnityEvent_1.cs:53
UnityEngine.UI.Toggle.Rebuild (executing=UnityEngine.UI.CanvasUpdate.Prelayout) in C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\UI\Core\Toggle.cs:85
UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () in C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\UI\Core\CanvasUpdateRegistry.cs:122
UnityEngine.Canvas.SendWillRenderCanvases () in C:\buildslave\unity\build\artifacts\generated\common\modules\UI\UICanvasBindings.gen.cs:206
My current code (only called when I stop the Play mode) looks like this
public void toggleMuteMusic(){
masterMixer.SetFloat("musicVol", -80f);
PlayerPrefs.SetFloat("musicVol", -80f);
if(musicVolSlider.interactable){
masterMixer.SetFloat("musicVol", musicVolSlider.value);
PlayerPrefs.SetFloat("musicVol", musicVolSlider.value);
}
musicVolSlider.interactable = !musicVolSlider.interactable;
}
Although I put PlayerPrefs.Save() into a different function ONLY to be called by a GUI button, when I start the game the second time withouth doing anything else, the vol will be what the inscene slider values are set to (-10). The moment I quit the game, the registry entry will be set (why??). It seems it has to do with the initial value of the slider and enabled/disabled objects. If I add another variable called muteMusic and start the game, even this will be set to true because for some reason, the Toggle function is being called the moment I quit the game. This doesnt make sense.
My PlayerPrefs are saved even if I never clicked the button that calls the Save() function.
Initial slider value is -10, script loads -11 if no PlayerPrefs are found. First start it loads -11, then I quit without doing anything, next times it loads -10 although this value was never Save()d.
Does anyone have any ideas how to circumvent this and what exactly is happening here?