- Home /
Hey I have problem with my music game script audio source,Hello I have problem with my MusicAudioSource script
Hello I have a problem with my music script, Everything is fine when I change scenes the music still runs with the same volume from the slider that I made but when I go back to the options panel It did not save my Audio mixer in my slider and even not giving me the option to toggle the music on and off... I tried to fix it with many ways but it made other issues.
This is my script:
bool musicOn = true;
public float musicVolume = 1f;
static AudioScript instance = null;
void Start()
{
if (PlayerPrefs.HasKey("MusicEnabled"))
{
if (PlayerPrefs.GetString("MusicEnabled") == "True")
{
musicOn = true;
}
else if (PlayerPrefs.GetString("MusicEnabled") == "False")
{
musicOn = false;
}
}
if (instance != null && instance != this)
{
Destroy(gameObject);
}
else
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
TryPlayMusic();
}
public void TryPlayMusic()
{
if (musicOn)
{
GetComponent<AudioSource>().Play();
}
else
{
GetComponent<AudioSource>().Stop();
}
}
// Script for the button.
public void ToggleMusic()
{
if (musicOn)
{
musicOn = false;
PlayerPrefs.SetString("MusicEnabled", "False");
}
else
{
musicOn = true;
PlayerPrefs.SetString("MusicEnabled", "True");
}
TryPlayMusic();
}
// For the slider
public void SetVolume(float volume)
{
GetComponent<AudioSource>().volume = volume;
}
I don't know what's the problem here and how can I save the volume slider... When I go back to the options panel scene after I toggle on music and changed the volume I try to toggle music again or play with the slider and It says : "Can not play a disabled audio source"
Your answer
Follow this Question
Related Questions
Audio Clip Playing every frame 2 Answers
C# | How to delay a method with parameters 2 Answers
Glitched Music 0 Answers
How do I get enemies to only chase me in certain areas 0 Answers