I’ve got this problem with the UI sliders. i use 2 of them for sound en music volume and i wrote this script. when the scene starts the script gets the playerprefs values en set them to the slider. but in the function: SetSliders(), The function only excecutes the first line of the 2. i even tried switching them. only the first line.
using UnityEngine;
using UnityEngine.UI;
public class OptionsSoundScript : MonoBehaviour
{
public Slider soundSlider;
public Slider musicSlider;
void Start ()
{
SetSliders();
}
//mute a value
public void Mute(string name)
{
PlayerPrefs.SetFloat(name, 0);
SetSliders();
}
//get the value of the slider en store in playerprefs
public void GetSliders()
{
PlayerPrefs.SetFloat("MusicVolume", musicSlider.value);
PlayerPrefs.SetFloat("SoundVolume", soundSlider.value);
}
//get the value of the playerpref and set the value of the slider
void SetSliders()
{
soundSlider.value = PlayerPrefs.GetFloat("SoundVolume");
musicSlider.value = PlayerPrefs.GetFloat("MusicVolume");
}
}
I tried this and like you only noticed the first ui Element being executed. After writing an edited version of your script. I realised that Unity actually got into some sort of loop for some reason, preventing the next line from executing.
To fix this, in the unity editor. Click on the UI element, click Add Component, type “Event Trigger” and add an Event Trigger. Then click “Add New Event Type”, then add “PointerUp” and use that to execute the script rather than OnValueChanged trigger.
Btw here’s my edited script
- Nothing is really changed significantly.
public class OptionsSoundScript : MonoBehaviour {
public UIElements uiElements = new UIElements();
[System.Serializable]
public class UIElements
{
public Slider soundSlider;
public Slider musicSlider;
}
void Awake ()
{
SetSliders();
}
public void Mute (string name)
{
PlayerPrefs.SetFloat(name, 0);
SavePlayerPrefs();
SetSliders();
}
public void GetSliders ()
{
PlayerPrefs.SetFloat("MusicVolume", uiElements.musicSlider.value);
PlayerPrefs.SetFloat("SoundVolume", uiElements.soundSlider.value);
SavePlayerPrefs();
}
public void SetSliders ()
{
uiElements.soundSlider.value = PlayerPrefs.GetFloat("SoundVolume");
uiElements.musicSlider.value = PlayerPrefs.GetFloat("MusicVolume");
}
public void SavePlayerPrefs()
{
PlayerPrefs.Save();
}
}
I fixed it myself, a bit dump i didn’t tested this way. Changed the code for the OnValueChange trigger and in the UpdateSlider function i broke the code down in 2 steps and now it works.
using UnityEngine;
using UnityEngine.UI;
public class OptionsSoundScript : MonoBehaviour
{
public Slider soundSlider;
public Slider musicSlider;
void Start ()
{
UpdateSliders();
}
//mute a value
public void Mute(string name)
{
PlayerPrefs.SetFloat(name, 0);
}
//set the sound volume through slider
public void SetSound(float a)
{
PlayerPrefs.SetFloat("SoundVolume", a);
}
//set the music volume through slider
public void SetMusic(float a)
{
PlayerPrefs.SetFloat("MusicVolume", a);
}
void UpdateSliders()
{
float soundValue = PlayerPrefs.GetFloat("SoundVolume");
float musicValue = PlayerPrefs.GetFloat("MusicVolume");
soundSlider.value = soundValue;
musicSlider.value = musicValue;
}
}