Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove extra Audio Listeners when loading scenes

I'm preloading a new scene using SceneManager.LoadSceneAsync to create an animation exit effect, but this gives me the error:

There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.

How can I ensure I have only one audio listener on my scene?

    // Public function to change Scene
    public void GoToScene(string goToScene)
    {
        // Starts exit animation and changes scene
        CanvasAnimation.SetBool("hide", true);
        StartCoroutine(ChangeScene(ExitTime, goToScene));
    }

    IEnumerator ChangeScene(float time, string goToScene)
    {
        //Set the current Scene to be able to unload it later
        Scene currentScene = SceneManager.GetActiveScene();

        // The Application loads the Scene in the background at the same time as the current Scene.
        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(goToScene, LoadSceneMode.Additive);
        asyncLoad.allowSceneActivation = false;

        yield return new WaitForSeconds(time);

        asyncLoad.allowSceneActivation = true;

        //Wait until the last operation fully loads to return anything
        while (!asyncLoad.isDone)
        {
            yield return null;
        }

        //Move the GameObject (you attach this in the Inspector) to the newly loaded Scene
        SceneManager.MoveGameObjectToScene(ObjToSave, SceneManager.GetSceneByName(goToScene));

        //Unload the previous Scene
        SceneManager.UnloadSceneAsync(currentScene);
    }

Thanks for your help

like image 738
Chico3001 Avatar asked May 03 '26 18:05

Chico3001


1 Answers

You can ensure you oily have one AudioListener by checking all your cameras. They are usually automatically attached to a newly create camera. Check each camera and remove it. You only need one AudioListener attached to your main camera.

You can also do this from code:

Find all instances of AudioListener in the scene with FindObjectsOfType then remove them if they are not attached to the MainCamera. You can find out if the AudioListener is attached to the main camera or not by checking its tag name which should be "MainCamera" by default.

AudioListener[] aL = FindObjectsOfType<AudioListener>();
for (int i = 0; i < aL.Length; i++)
{
    //Destroy if AudioListener is not on the MainCamera
    if (!aL[i].CompareTag("MainCamera"))
    {
        DestroyImmediate(aL[i]);
    }
}

Sometimes, you may have multiple cameras with the "MainCamera" tag. If this is the case, leave the first AudioListener returned by FindObjectsOfType but destroy the AudioListener in the array. You leave one because it is required to play sound in the scene.

AudioListener[] aL = FindObjectsOfType<AudioListener>();
for (int i = 0; i < aL.Length; i++)
{
    //Ignore the first AudioListener in the array 
    if (i == 0)
        continue;

    //Destroy 
    DestroyImmediate(aL[i]);
}

Note that the Destroy function should be fine too. I chose DestroyImmediate to remove it immediately instead of doing it in another frame.

like image 69
Programmer Avatar answered May 06 '26 08:05

Programmer