Hi,
I’m currently designing my games Options and have finalized my Audio sliders. My Issue though is in the TMP field for the text I have input xxx% as a placeholder with this script attached:
public class AudioController : MonoBehaviour
{
public string VolumeType = "Volume";
public AudioMixer Mixer;
public Slider Slider;
public TextMeshProUGUI VolumeText;
public float Multiplier = 30f;
[Range(0,1)] public float DefaultSliderPercentage = 0.75f;
private void Awake()
{
if(!PlayerPrefs.HasKey(VolumeType))
{
PlayerPrefs.SetFloat(VolumeType, DefaultSliderPercentage);
}
Slider.onValueChanged.AddListener(SliderValueChanged);
Slider.value = PlayerPrefs.GetFloat(VolumeType);
}
public void SliderValueChanged(float sliderValue)
{
Mixer.SetFloat(VolumeType, SliderToDecibel(sliderValue));
VolumeText.text = Mathf.Round(sliderValue * 100) + "%";
PlayerPrefs.SetFloat(VolumeType, sliderValue);
PlayerPrefs.Save();
}
private float SliderToDecibel(float value)
{
return Mathf.Clamp(Mathf.Log10(value/DefaultSliderPercentage) * Multiplier, -80f, 20f);
}
}
My problem is that when starting the game the text shows as “xxx%” until i move the slider which then updates correctly. How do i make it show the correct number when starting the game?