- Home /
Slider value saving?
I want to save my slider's value so when I change scences, the values remain the same and also retarting the game, it reamins the same. Here is my code so far which is on an empty game object with the sliders for X and Y sensitivity in it. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class sliderSaves : MonoBehaviour { public Slider XSlider; public Slider YSlider; // Use this for initialization void Start () { PlayerPrefs.SetFloat("OptionScore", XSlider.value); PlayerPrefs.SetFloat("OptionLives", YSlider.value); XSlider.value = PlayerPrefs.GetFloat("OptionScore"); YSlider.value = PlayerPrefs.GetFloat("OptionLives"); PlayerPrefs.Save(); }
}
Answer by awaismazhar999 · Jun 15, 2017 at 11:56 AM
Dear @username There are two ways for storing the value of sliders. one is by using PlayerPrefs and other by using using Scriptable Objects. As far as your code is concerned, in Start method you first getting the values of sliders and storing them in PlayerPrefs, after that you assign this stored value to sliders. this will always get value of 0.0f of or some other value you set it on inspector. During Gameplay on changing the the value of slider will not change its value in PlayerPrefes. To avoid this problem you can store values in Update method instead of Start Method like given below:-
void Start()
{
XSlider.value = PlayerPrefs.GetFloat("OptionScore");
YSlider.value = PlayerPrefs.GetFloat("OptionLives");
}
void Update()
{
PlayerPrefs.SetFloat("OptionScore", XSlider.value);
PlayerPrefs.SetFloat("OptionLives", YSlider.value);
}
Your answer
Follow this Question
Related Questions
Problems with pulling a slider value - (Making sensitivity Sliders) 0 Answers
Getting a slider value from a different scene 1 Answer
My mouse sensitivity value don't update in other function than sliderValueChange 0 Answers
Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 1 Answer
Slider value not working? 0 Answers