I have been working on this for a while now and cannot figure it out! This cannot be that difficult and I may be overlooking something very simple. When I start the game the audio plays fine and stops when I go into gameplay, but if I quit from the gameplay scene and go back to the main menu the audio does not play. Why would this be happening?
Here’s my code:
using UnityEngine;
using System.Collections;
using UnityEngine .SceneManagement ;
public class AudioManager : MonoBehaviour {
public AudioSource BGM;
static bool AudioBegin = false;
// Use this for initialization
void Awake () {
}
void Start(){
if (AudioBegin == false) {
BGM.Play ();
DontDestroyOnLoad (gameObject);
AudioBegin = true;
}
/*if (FindObjectsOfType <AudioManager > ().Length>1) {
Destroy (gameObject);
}*/
}
// Update is called once per frame
void Update () {
if ((SceneManager.GetActiveScene ().name == "Title Menu" && AudioBegin == false)){
BGM.Play ();
DontDestroyOnLoad (gameObject);
AudioBegin = true;
}
if ((SceneManager.GetActiveScene ().name=="Gameplay") && AudioBegin == true) {
BGM.Stop ();
DontDestroyOnLoad (gameObject);
AudioBegin = false;
}
if (FindObjectsOfType <AudioManager > ().Length>1) {
Destroy (gameObject);
}
}
}