- Home /
How to load audio slider values from PlayerPrefs at the beginning of the game?
Hi everyone!
I have a slight issue with my game , specifically with my audio slider.
The saved audio value is being loaded only when I'm navigating into my menu scenes audio menu (it has the slider childed to it).
The desired function is to load the audio value at the beginning of the game in the start scene but I couldn't figured it out yet how to do it.
Any help is appreciated! :)
Here is the script of my slider:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
public class MusicSlider : MonoBehaviour {
public AudioMixer audioMixer;
public Slider musicSlider;
public void Start()
{
musicSlider.value = PlayerPrefs.GetFloat("Music",1f);
}
public void AdjustVolume(float volume)
{
PlayerPrefs.SetFloat("Music", volume);
audioMixer.SetFloat("Music", Mathf.Log10(volume) * 20);
}
}
Answer by ray2yar · Dec 31, 2018 at 02:26 PM
Before your music starts, get a volume value from playerprefs. Whenever the player changes the volume during the game set the value in the playerprefs. On game start get the value.
Thanks for your fast reply.
I tried before to get the value in my start scene in an awake function and It changed the audio sources value but the mixers value was the same and the music was just as loud as it's default value.
Can you please give me a sample code how it looks like?
Someone just posted code that will work.
$$anonymous$$aybe you're seeing an effect of the slider at value = 0. The result can be a maximum volume in the mixer. Try setting the lower limit of the slider to something really small, but not 0.
Here is what I wrote up anyways, pretty much the same as the code Tobychappell put in.
public Slider VolumeSlider;
public Audio$$anonymous$$ixer Aud;
float volume;
// Use this for initialization
void Start () {
if (PlayerPrefs.Has$$anonymous$$ey("$$anonymous$$usic"))
{
volume = PlayerPrefs.GetFloat("$$anonymous$$usic");
VolumeSlider.value = volume;
}
}
public void OnVolumeChanged()
{
volume = VolumeSlider.value;
Aud.SetFloat("$$anonymous$$usic", $$anonymous$$athf.Log10(volume) * 20);
PlayerPrefs.SetFloat("$$anonymous$$usic", volume);
}
Answer by Tobychappell · Dec 31, 2018 at 04:02 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
public class MusicSlider : MonoBehaviour {
public AudioMixer audioMixer;
public Slider musicSlider;
public void Start()
{
musicSlider.value = PlayerPrefs.GetFloat("Music",1f);
// Set it at the start <<<<
audioMixer.SetFloat("Music", Mathf.Log10(musicSlider.value) * 20);
}
public void AdjustVolume(float volume)
{
PlayerPrefs.SetFloat("Music", volume);
audioMixer.SetFloat("Music", Mathf.Log10(volume) * 20);
}
}
Unfortunately I still can't figure it out how to get the values in my start scene.
When I try to access the slider from a scrpit in my start scene I'm getting a null reference exception.
Any ideas for that?
Well that's thrown when it isn't assigned. Did you assign the slider in the inspector or getcomponent?
I have created a script in my start scene to load the audio value , I tried to find the slider with getcomponent but I still get the null reference exception.
Should I prefab the slider?
Your answer
Follow this Question
Related Questions
PlayerPrefs not saving slider data 3 Answers
Slider value not working? 0 Answers
UI Slider, Save Values and Change Them 0 Answers
Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 1 Answer
Volume Slider 0 Answers