I created 1 track for the main menu and 1 track for the game which continues for all my levels, lose screen and win screen.
The main menu track is correctly being removed and game music starts when I hit start game from the main menu.
The Lose screen and win screen have a return to Main menu button, when returning to the main menu, the game music continues and the main menu music fires up again. How can I remove the game music at main menu again?
I have an AudioListener game Object on each screen and the script attached to each. I only have the sound attached to an audio source component added to the Main Menu and the Level 0 (next scene i.e. game start)
Code for audioListener
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AudioListener : MonoBehaviour
{
AudioSource audioSource;
void Start()
{
Scene currentScene = SceneManager.GetActiveScene();
string sceneName = currentScene.name;
if (sceneName != "Main Menu")
{ DontDestroyOnLoad(this);
audioSource = GetComponent<AudioSource>();
}
else
{ Destroy(this); }
}
}
I think when it returns back to the main menu, the game audio source is still playing, the DontDestroyOnLoad is still active.
I have 1 audio Source with Play on awake as below for a scene.
And no I don’t use the Play() anywhere. The script above is the only audio script I have unless there are jump/death sounds attached to objects calling an SFX.
You need only one AudioListener per scene. It acts as the ears, so to say.
Regarding the AudioSources, you’ll have to decide what you want to achive. Instead of using “Play On Awake”, you could control the AudioSource via code with Play(), Pause() and/or Stop().
And if the clip depends on the scene, use the onSceneLoaded delegate. Take a look at the example in the API.
I just finished up on the TileVania now adding in more levels and additional sounds which were not covered in that part. In the course had done Singletons to keep 1 track playing but not like this. I think this might be covered in the 3D course but I’m only starting that next month, 1st completing the ones I’ve made.
I updated the script as below using the Play() also trying to rather create some type or array.
public class AudioListener : MonoBehaviour
{
static AudioListener music;
[SerializeField] AudioClip[] musicList;
private void Awake()
{
if (!music)
{
music = this;
}
else
Destroy(this.gameObject);
DontDestroyOnLoad(this.gameObject);
}
public void ChangeMusic(int audioClipIndex)
{
AudioSource audio = GetComponent<AudioSource>();
audio.clip = musicList[audioClipIndex];
audio.Play();
}
But the music wont start and the next scene wont load with a NullReference to my game object on loading scene. I added the reference to load the music in the Menu script
public class Menu : MonoBehaviour
{
GameObject musicPlayer;
public void StartFirstLevel()
{
musicPlayer.GetComponent<AudioListener>().ChangeMusic(1);
SceneManager.LoadScene(1);
}
public void MainMenu()
{
musicPlayer.GetComponent<AudioListener>().ChangeMusic(0);
SceneManager.LoadScene(0);
}
If Menu throws a NullReferenceException it’s probably because nothing was assigned to the musicPlayer variable. At least I cannot see where something gets assigned.