How can i play audio clips from some audio sources at the same time ? (SOLVED)

In my main menu I’m playing a audio clip in audio source component. The problem is that in my space station all the doors have also audio source components with another audio clips that play the doors effects open/close.

Now what I’m doing is to Stop all the audio sources in the space station once I’m in the main menu. If not the music in the main menu will not playing.

This is a way to by pass the problem but not a solution since later in the game i will want to play a music in the background and also to use the doors audio effects at the same time.

How can i fix this conflict/s ?

This is the MainMenu script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainMenu : MonoBehaviour
{
   public GameObject player;
   public Transform audioParentToSearch;
   public bool mainMenu = false;

   // Use this for initialization
   void Start()
   {
       Main();
   }

   // Update is called once per frame
   void Update()
   {
       if (Input.GetKeyDown(KeyCode.Escape))
       {
           Main();
           mainMenu = true;
       }
   }

   void ProcessAudios(Transform parent, int level = 0)
   {
       foreach (Transform child in parent)
       {
           if (child.GetComponent<AudioSource>() != null)
           {
               AudioSource audio = child.GetComponent<AudioSource>();
               audio.Stop();
           }

           // Process next deeper level
           ProcessAudios(child, level + 1);
       }
   }

   public void Main()
   {
       if (mainMenu == false)
       {
           ProcessAudios(audioParentToSearch);
           player.SetActive(false);
       }
   }
}

The first screenshot showing the Main Menu audio source in the inspector it’s playing a music ascension:

The second screenshot showing one of the doors Inspector audio source:

The problem I can only guess is that it can’t play from the same source so many audio clips. But that is only a guess.

From what I tested if I stop the audio sources on the doors or on every place in the space station it will play the music in the main menu but if any of the audio sources on the space station are playing it will not play or it will not hear the main menu music.

In most of the game or some of them you can play and hear both music and sound effects at the same time.

I use AudioListener.pause and AudioSource.ignoreListenerPause.

1 Like

I solved it.

No need all the loop and stuff I did in the main menu script.
What i did is first in the editor in the Hierarchy turned off all audio listeners and left only one on the one on Main Camera.
Then all audio sources set on and playing and i just played with the Music sound Priority value.

The music Priority is set to 60. The doors and other effects the Priority set to 128.
So the music is playing the in the background and the effects a bit louder.

Now i will add this options to the my Main Menu > Options > Audio
So the user will be able to change on his own the Priority between the effects and music.

Working great now.