I am working on a 2D Angry Birds type Unity game. I Created Menu Scene and Level One Scene and all the required scripts are added. After adding background music to Level One, when the game is over and return to Menu I can't stop the music. The Menu scene has its own music and when the level one is over, it continues with its own music and gets mix with the menu scene music. I tried to add Destroy()
function to level one GameObject but Level One music became silent after I restart with my menu scene. Here is my code for the attached music game object:
public class background_sound : MonoBehaviour {
public static bool soundBreaker = false;
public void Start () {
if (!soundBreaker) {
AudioSource myAudio = GetComponent<AudioSource> ();
myAudio.Play ();
DontDestroyOnLoad (myAudio);
soundBreaker = true;
}
}
}
And here is the Menu Script:
public class MenuScript : MonoBehaviour {
public void StartGame () {
SceneManager.LoadScene ("Level One");
}
...
Here is the code that loads the Menu Scene..
public void GameOver() {
musicObject.setActive(false);
startCoroutine(restTime());
}
IEnumerator restTime() {
yield return new WaitForSeconds (3);
SceneManager.LoadScene("Menu");
}