- Home /
master background music toggle not working after changing scenes
i have background music that plays throughout all of my scenes,
i have added "on" and "off" music buttons to a settings panel in my mainmenu scene
currently the "on" "off" music buttons work if i start the game in the mainmenu scene, however once i change scenes and go back to main menu,
my "on" "off" buttons no longer work
i will eventually be adding sound effects and will hope to be able to also turn off sound effects and music separately, but would like to get the background music working first
public class SoundManager : MonoBehaviour {
public AudioSource musicSource;
public static SoundManager instance = null;
void Awake ()
{
if (instance == null)
instance = this;
else if (instance != this)
Destroy (gameObject);
if (!musicSource.isPlaying)
{
DontDestroyOnLoad (gameObject);
}
}
public void PlayMusic()
{
musicSource.Play();
}
public void PauseMusic()
{
musicSource.Pause();
}
}
this is attached to an empty game object called sound$$anonymous$$anager in my hierarchy
with "on" and "off" buttons OnClick calling the sound$$anonymous$$anager AudioSource.Play and .Pause
I'm new to unity and program$$anonymous$$g, any help would be GREATLY appreciated.
Answer by hexagonius · Sep 08, 2017 at 03:24 AM
You can't combine a singleton with an in game menu. The second time you run main it's still the first singleton instance, but since the buttons directly map to the scene object, which gets destroyed, nothing happens.
Let the buttons call methods in the scene which in turn call the singleton.