1

Currently I have my toggle to On Value Change make AudioSource.enabled, which does turn the music off.

However when I die in the game the level gets reloaded causing the toggle to go back to its default state and then i am unable to turn the music back on.

So what is the best way to make a music toggle and have the state of the toggle be remembered on load?

Grant Winney
  • 61,140
  • 9
  • 100
  • 152
Zyie
  • 124
  • 2
  • 12

2 Answers2

2

Another solution would be a singleton music manager game object that is created only once and is persistent between scenes, meaning that it will not be destroyed when you reload a scene or load a different scene.

You can read more about Singleton patterns here: http://unitypatterns.com/singletons/

Varaquilex
  • 3,312
  • 7
  • 39
  • 57
  • I'm fairly new to design patterns and this solution feels just right. Besides, why not :P ? – Varaquilex Jan 26 '15 at 19:31
  • My comment wasn't specific to your answer(If I'd consider wrong it, I usually downvote explaining why). It's a just a personal disappointment because of hearing too much suggestion of using singletons.. singletons are often bad for several reasons, just google for some considerations on the topic or start from here: http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons . – Heisenbug Jan 26 '15 at 20:05
0

You can save persistent data across scenes using PlayerPrefs. Just store there the audio status and be sure to check if it's enabled when loading a new scene (for example inside Awake method of a MonoBehaviour attached on the same AudioSource's GameObject).

Side note

Depending on the specific situations, turn off globally the volume could be easier using AudioListener.volume rather than modifying all AudioSources.

Heisenbug
  • 37,414
  • 27
  • 126
  • 181
  • Would you be able to tell me how to actually store the audio status and how i would get the toggle to stay on or off after reload? – Zyie Jan 24 '15 at 20:17