- Home /
PlayerPrefs not saving slider data
Im trying to save my two sliders values to the playerprefs. It works until I end play mode and start again. Then its back to the default values. If I quit my game and go to the options menu while still in play mode it doesnt work then either. Is there something Im missing. Im very new to PlayerPrefs.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Options : MonoBehaviour {
public Slider scoreSlider;
public Slider livesSlider;
public Text scoreSliderText;
public Text livesSliderText;
void Start()
{
PlayerPrefs.GetFloat("OptionScore");
PlayerPrefs.GetFloat("OptionLives");
}
void Update()
{
PlayerPrefs.SetFloat("OptionScore", scoreSlider.value);
PlayerPrefs.SetFloat("OptionLives", livesSlider.value);
scoreSliderText.text = "" + PlayerPrefs.GetFloat("OptionScore");
livesSliderText.text = "" + PlayerPrefs.GetFloat("OptionLives");
scoreSlider.value = PlayerPrefs.GetFloat("OptionScore");
livesSlider.value = PlayerPrefs.GetFloat("OptionLives");
PlayerPrefs.Save();
}
}
Changing PlayerPrefs from Update is a bad idea. It looks to me like your code is fine but Update runs several times per frame. You're probably saving values from some point during it's destruction.
Logically you only need to do this once each time you get rid of this particular component.
Answer by DiegoSLTS · Feb 10, 2015 at 06:06 PM
You're saving the slider.value on every Update, when you run the game the slider is at the default position and the first Update saves that position, overwriting the one you saved before.
I see you're getting those values on the Start method, I guess it's to initialize the sliders to the values stored in PlayerPrefs, but you're just getting the values, you're not doing anything with them.
Try with this Start method:
void Start()
{
scoreSlider.value = PlayerPrefs.GetFloat("OptionScore");
livesSlider.value = PlayerPrefs.GetFloat("OptionLives");
}
Also, storing the values on EVERY Update looks like a bad idea (a waste of resources and may be the source of bugs), look at the onValueChanged property of the slider, you can even set it up in the Inspector.
Thank you, that fixed it. How do you use the onValueChanged property?
Select a slider on the hierarchy and look at the "Slider" component on inspector. At the bottom it has a section that says "On Value Changed" with a + and - at the bottom right. Press the + button and it'll let you choose a game object, then any script on that game object, and then any function that can be used.
You can add multiple functions to this and they'll be called one after another.
Answer by giulio-pierucci · Feb 10, 2015 at 06:24 PM
You should't use PlayerPrefs.Save in Update! By the Way, i think the problem is in Start():
void Start()
{
scoreSlider.value = PlayerPrefs.GetFloat("OptionScore");
livesSlider.value = PlayerPrefs.GetFloat("OptionLives");
}
Answer by TooManySugar · May 10, 2017 at 01:37 PM
Something I've noticed (unity 4.7.2) Is that by the time you whant to save slider values to player prefs you've to do it individually rather than in bulk, this way
public void ChaseCamDist_Save(){
distance = DistanceSlicer.value;
Debug.Log ("ChaseCamDist" + PlayerPrefs.GetFloat("ChaseCamDist"));
PlayerPrefs.SetFloat ("ChaseCamDist", distance);
}
public void ChaseCamHeight_Save(){
height = HeightSlider.value;
Debug.Log ("ChaseCamHeight" + PlayerPrefs.GetFloat("ChaseCamHeight"));
PlayerPrefs.SetFloat ("ChaseCamHeight",height);
}
I was saving this two sliders with a single func and allways the second one failed and got the devault value instead. This happened me in teh past too.
Your answer
Follow this Question
Related Questions
How to save slider values? 3 Answers
Problems with pulling a slider value - (Making sensitivity Sliders) 0 Answers
PlayerPrefs Resets Automatically upon returning to the Main Menu 2 Answers
Getting a slider value from a different scene 1 Answer
Why does my slider properties go missing when switching scenes ? 1 Answer