- Home /
How to save slider values?
I was wondering if anyone had any way to save slider values i dont really have a script cause all the things i have tried will not work cause i cant import the slider into the actual reference box on the script and it will not let me directly find the slider in the script.
Share some code of what you tried, and some pics of the setup of the slider, so we can see what you did wrong and how to fix it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Slidersaver : $$anonymous$$onoBehaviour
{
public Slider xslider;
void Start()
{
xslider = GameObject.Find("Slider").GetComponent<Slider>().value;
xslider.value = PlayerPrefs.GetFloat("OptionScore");
}
void Update()
{
PlayerPrefs.SetFloat("OptionScore", xslider.value);
}
}
You can actually save your slider values in your Slider.SetLevel(float value)
function. Also, I dont know why you have Slidersaver class (what you share) and ins$$anonymous$$d your slider has attached SavingSlider which is another different class.
Since your slider is public
, you don't need to call Find
, just drag & drop the gameObject holding the slider into the field. It's even more interesting since the script is on the same object as the slider itself.
If we forget this point, What does not work with your current script?
Answer by zereda-games · Mar 10, 2019 at 04:40 PM
public class PlayerPrefsManager{
const string MasterVolumeSaveKey ="Master Volume";
public static float Volume;
/// <summary>
/// Gets the master volume.
/// </summary>
/// <returns>The master volume.</returns>
public static float GetMasterVolume ()
{
Volume = PlayerPrefs.GetFloat (MasterVolumeSaveKey);
return Volume;
}
/// <summary>
/// Sets the master volume.
/// </summary>
/// <param name="value">Value.</param>
public static void SetMasterVolume (float value)
{
if (value >= 0f && value <= 1f) {
PlayerPrefs.SetFloat (MasterVolumeSaveKey, value);
} else {
Debug.LogException (new System.Exception ("Music volume slider out of range. Set slider between 0 and 1 and Not whole numbers."));
}
}
}
to use:
public Slider VolumeSlider;
public static float volume;
void Update(){
volume = PlayerPrefsManager.GetMasterVolume("MasterVolume");
OnChangeVolume(VolumeSlider.value);
}
void OnChangeVolume(float value){
PlayerPrefsManager.SetMasterVolume("Master Volume", VolumeSlider.value);
}
@zereda-games Iv used this but after i reference the slider it wont let me drag my slider into the reference box on the script once in the manager is there a specific reason for this
Answer by gpuelston2004 · Mar 10, 2019 at 12:34 AM
Okay thanks for the comments but the reason i have 2 different things calling the slider is because nither of the ways i tryed worked but thanks for thr help i will try that
Answer by DawdleDev · Mar 10, 2019 at 04:26 PM
Here's your problem. You never use Playerprefs.Save(). Try adding this to the script to make it save each time you load a new scene.
private void OnDisable () {
Playerprefs.Save ()
}
Everything else here seems good to go.
Your answer
Follow this Question
Related Questions
UI Slider, Save Values and Change Them 0 Answers
Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 1 Answer
Updating UISlider with value from variable. 1 Answer
How to lock a slider unless the handle is being interacted with? 0 Answers
How do i move the slider with GVR reticle pointer? 0 Answers