Question by
StroBerri · Jul 12, 2021 at 08:29 AM ·
playerprefsvolume
Value returns to default upon exiting the game
I have a volume slider that only loads the PlayerPrefs once i clicked the button. I want it to load once the game starts. I'm only 1 month with unity so I'm really a baby with this one. Here's the code I used. Thanks In advanced for someone who can help. <3
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class SoundSlider : MonoBehaviour {
[SerializeField] Slider volumeSlider;
void Start()
{
if (!PlayerPrefs.HasKey("musicVolume"))
{
PlayerPrefs.SetFloat("musicVolume", 0);
Save();
}
else
{
Load();
}
}
public void ChangeVolume()
{
AudioListener.volume = volumeSlider.value;
Save();
}
private void Load()
{
volumeSlider.value = PlayerPrefs.GetFloat("musicVolume");
}
private void Save()
{
PlayerPrefs.SetFloat("musicVolume", volumeSlider.value);
}
}
Comment