- Home /
Background music doesn't start on new scene load.
Hey all,
So I was looking for a way to continue my BGM when the player hits the restart button. I've managed to solve this by creating a game object with a DontDestroyOnLoad line in the code, and it works fine whenever I hit the start button.
The code for that looks like this (on my Audio game object):
private void Awake()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("Music");
if (objs.Length > 1)
{
Destroy(this.gameObject);
}
DontDestroyOnLoad(this.gameObject);
The problem comes in when the player instead of hitting restart, goes back to the main menu, and then re-enters the scene - then the audio doesn't start up again.
In my game manager, when the player hits the end game button to go back to the main menu - this happens:
public void EndGame()
{
GameObject.Find("Music").GetComponent<AudioSource>().Stop();
SceneManager.LoadScene("Title screen");
}
I'm assuming my issue is to do with the fact that this code stops the audiosource, however the Audio game object has this code on start so it should start up again when re-loading the scene?
void Start()
{
music = GetComponent<AudioSource>();
music.Play();
}
It's also set to play on awake. Not sure exactly how to solve this one, any help would be appreciated but I bet its something obvious that i'm missing.
Tl;dr: Audio restarts as intended when the player hits "restart" but when exiting to the main menu and re-entering the scene that way the audio doesn't start again.
Your answer
Follow this Question
Related Questions
Background music doesn't start on new scene load. 0 Answers
Audio restarting during LoadLevel 1 Answer
Use audio to change the emission of an object. 0 Answers
Timeline Audio Source problem using Cinemachine 1 Answer
Trigger audio clip on load level async (only playing for a fraction of a second) 0 Answers