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.