- Home /
PlayerPrefs not saving slider value & audio
for some reason, my slider and audio resets back to 1 when I stop and play the game after I changed the value. Here is the code, I can't spot anything wrong with it:
//sliderText.cs - created by Kyle 06 Jan 2015
//Attach to option sliders
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class sliderText : MonoBehaviour {
public AudioSource music;
public Slider Master;
public Slider MusicSlider;
public Slider Voice;
public Slider SFX;
public Slider MouseSpeed;
public Slider FOV;
public Text MasterTxt;
public Text MusicTxt;
public Text VoiceTxt;
public Text SFXTxt;
public Text MouseSpeedTxt;
public Text FOVTxt;
/*public Text Text;
public Slider Slider;*/
void Start(){
audio.volume = PlayerPrefs.GetFloat("CurVol");
MusicSlider.value = audio.volume;
MusicTxt.text = "" + MusicSlider.value;
}
// Update is called once per frame
void Update () {
//Text.text = "" + Slider.value;
MasterTxt.text = "" + Master.value;
MusicTxt.text = "" + MusicSlider.value;
VoiceTxt.text = "" + Voice.value;
SFXTxt.text = "" + SFX.value;
MouseSpeedTxt.text = "" + MouseSpeed.value;
FOVTxt.text = "" + FOV.value;
}
public void VolumeControl(float volumeControl){
audio.volume = volumeControl;
PlayerPrefs.SetFloat("CurVol", audio.volume);
}
}
Comment
Best Answer
Answer by HarshadK · Jan 13, 2015 at 11:34 AM
You need to use PlayerPrefs.Save in order to make the PlayerPrefs be written to the disc.
Change your code to:
public void VolumeControl(float volumeControl){
audio.volume = volumeControl;
PlayerPrefs.SetFloat("CurVol", audio.volume);
PlayerPrefs.Save();
}
scrap that, apparently it only works when I build it xD I guess unity is being fussy
Answer by stenmats · Feb 26, 2016 at 03:14 PM
PlayerPrefs.SetFloat("CurVol", volumeControl); PlayerPrefs.Save();
}
im Sure because i got it;
Your answer
Follow this Question
Related Questions
Lagging/unmoving slider 0 Answers
Multiple Cars not working 1 Answer
Unity 4.6B UI Scrollbar Usage 1 Answer
4.6 UI "Slider" jumps on handle press? 2 Answers