- Home /
Question by
imagoculturae · Sep 27, 2018 at 02:21 PM ·
slider
Slider position does not update when value is loaded
Hi can't make this to work: Basically I am try to change the value of a slider when I load some value that I previously saved. I have tested the data and it works fine when reading it (the value is what is supposed to be) just does not update the position of the slider :-( Please help!
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/weatherInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/weatherInfo.dat", FileMode.Open);
WeatherData data = bf.Deserialize(file) as WeatherData;
file.Close();
DawnDuskSlider.value = data.hours;
Debug.Log("data in data is:" + data.hours);
Debug.Log("data in slider is:" + DawnDuskSlider.value);
}
}
Comment
Answer by KittenSnipes · Sep 29, 2018 at 09:19 AM
@paganic I think it is because you close the file and thus lost the data that it was holding temporarily. Try writing it like this instead:
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/weatherInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/weatherInfo.dat", FileMode.Open);
WeatherData data = bf.Deserialize(file) as WeatherData;
DawnDuskSlider.value = data.hours;
file.Close();
}
}
Nop... the slider still does not move :-(
I have also tried this but no luck when calling the function so that value is set and ok but slider does not move:
public void SubmitSliderSetting(float slide)
{
DawnDuskSlider.value = slide;
//Displays the value of the slider in the console.
Debug.Log(DawnDuskSlider.value);
}
I think is a bug in Unity because I have also try this and it does not work:
public void SaveSettings()
{
PlayerPrefs.SetFloat("Hours", DawnDuskSlider.value);
Debug.Log("data out slider is:" + DawnDuskSlider.value);
}
public void LoadSettings(float val)
{
DawnDuskSlider.value = PlayerPrefs.GetFloat("Hours", val);
Debug.Log("data in slider is:" + DawnDuskSlider.value);
}