Background music without restarting when changing scenes

When I start the game I am in a main menu from which one can switch to the “Game” scene or the “Options” scene. In the options, it should be possible to adjust the background music volume using controls, but I’ve been trying for 2 weeks with all different methods!
I have already watched over 30 different tutorials and none of them led to a solution that would not lead to errors!
Either the music starts anew every time I change a scene from the options back to the main menu, or the music continues in the background with “DontDestroyOnLoad” but ignores the controls in the scene of the options because I start in the main menu and only close it the scene “Options” has to switch.
which is why the audio has to start directly in the main menu. But if I switch to the options, then I cannot adjust the volume because the game object does not exist in the “Options” scene because otherwise the music would be doubled. But I don’t know how I can change the volume of a game object that is in the “DontDestroyOnLoad”.
It is absolutely impossible for me to implement it in such a way that I have a music: It starts when the game is started in the main menu and continues when I switch to the “Options” scene to be able to regulate the volume of this music.
I don’t even know if it is even possible to change the volume of something that is in the “DontDestroyOnLoad” !?

1 Like

If you go the DontDestroyOnLoad route, you have the music player/manager object be the one which exists between scenes. Any scene which needs to talk to it, to for example adjust volume, just finds the existing object. FindWithTag is an easy way.

The music object should have the volume controls with it (for simplicity), be placed only in the Main Menu scene, and use DontDestroyOnLoad on it. The downside is that when testing another scene directly, it won’t have that object in it—it should only exist in the Main Menu, and then is carried along to other scenes. You can get fancier to work around this (testing whether X object exists in a scene) as well.

I got the error because, there is no

You mean also, i should make THW Volumen sliders at the begin from the mainmenu but very small scaled, so that the Uset can’t See it?
And i should scale it back to the default sizes, if the sliders are needed inside the Option scene?

Not sure exactly what you’re going for, but if you want to hide them, just turn off the Renderers or set the objects in question inactive.

A singleton approach would work well for this sort of application.

Create a singleton monobehaviour that is loaded when the game starts. Use the static accessor anywhere you need to adjust it.

public class MusicSingleton : MonoBehaviour {
  public AudioSource musicPlayer; // the bgm audio source and/or whatever else you need
  public static MusicSingleton instance { get; private set; }

  void Awake () {
    instance = this;
    DontDestroyOnLoad( gameObject );
  }
}

And then just access it wherever you need it like so:

MusicSingleton.instance.musicPlayer.volume = /* whatever */;
1 Like