Fadeout music after scene change

Any general topic can be discussed here

Fadeout music after scene change

Postby rlf_b » Tue Sep 19, 2017 10:21 pm

Hello there,

unfortunately I am having some problems when changing to a new scene and back.
I have two scenes, number 1 containing a menu music loop and number 2 containing game music loop.

Now when loading scene 2 I would like to fade the menu music loop to the game music loop.
When I am in the game I also need to be able to go back, therefore after we switch back to scene 1 I want to fade the game music loop back to the menu music loop.

I have tried several things like only having an audio controller in the first scene containing both audios and have it persistend.
Then the music in the second scene does not play at all.

I also tried to have the audio controller persist scene loading and also have an additionl audio controller in the second scene. But I get an error when changing the scenes 1->2->1
More than one instance of SingletonMonoBehaviour AudioController
UnitySingleton`1:_Awake(AudioController) (at Assets/AudioToolkit/Shared Auxiliary Code/UnitySingleton.cs:123)
SingletonMonoBehaviour`1:Awake() (at Assets/AudioToolkit/Shared Auxiliary Code/UnitySingleton.cs:269)
AudioController:Awake() (at Assets/AudioToolkit/AudioToolkit/AudioController.cs:2706)

Any idea on how I can have music fade out in the next scene that is loaded?

And in scene one I load scene twi like this
if (Input.GetKeyDown(KeyCode.Alpha2)) {
SceneManager.LoadScene("test_audio_scene2scene_2", LoadSceneMode.Single);

In scene two I load scene one like this
if (Input.GetKeyDown(KeyCode.Alpha1)) {
SceneManager.LoadScene("test_audio_scene2scene_1", LoadSceneMode.Single);
Posts: 3
Joined: Sun Jul 30, 2017 8:48 pm

Re: Fadeout music after scene change

Postby clockstone » Thu Oct 05, 2017 1:08 pm

When you work with persistent game objects and you load the same scene multiple times you can use a scheme that we call "Persistent Game Object Factory". It's basically a simple script that instantiates a (persistent) prefab but only if it doesn't exist already (by looking for the game object in the current scnee by its name). This way you make sure that only a single instance of a persistent object can exist at a time. YOu can then add this factory object to all of your scenes (this also allows you to enter your game from any scene, not just the start scene)

An alternative way is to have a game loading scene with all persistent object that you never load again.
User avatar
Site Admin
Posts: 17
Joined: Mon Jan 13, 2014 3:18 am

Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests