0
\$\begingroup\$

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");
} 
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Change your class to:

public class background_sound : MonoBehaviour {
    public static bool soundBreaker = false;

    public AudioSource audio;
    public void Start () {
        if (!soundBreaker) {
            audio = GetComponent<AudioSource> ();
            audio.Play ();

            soundBreaker = true;

            DontDestroyOnLoad(this.gameObject);
        }   
    }

    public void StopPlaying(){
        audio.Stop(); // or audio.Pause();
    }

    public void StartPlaying(){
        audio.Play();
    }
}

In your GameOver() do:

musicObject.GetComponent<background_sound>().StopPlaying();

And a tip: Don't name your classes with a lowercase character and don't add _ to it. It's a bad programming practice. Change it to BackgroundSound or something and rename it's file too.

\$\endgroup\$
3
  • \$\begingroup\$ I added your code but when it is game over and reloads level 1,the music becomes silent \$\endgroup\$
    – Haileapp
    Jun 28, 2017 at 19:19
  • \$\begingroup\$ Because in your Level's Start() you must call musicObject.GetComponent<background_sound>().StartPlaying(); \$\endgroup\$
    – Jacob
    Jun 28, 2017 at 19:23
  • \$\begingroup\$ I added start playing in Level 1 Start() but as the ball is reloaded it repeated the music before scene one is over. \$\endgroup\$
    – Haileapp
    Jun 28, 2017 at 19:42
0
\$\begingroup\$

Just remove the DontdestroyOnLoad

\$\endgroup\$
4
  • \$\begingroup\$ When I remove don't destroy,the music will start from the beginning within the same scene when it reloads the ball.I added the don't destroy to continue the music in that scene Only but it continues through all scenes. \$\endgroup\$
    – Haileapp
    Jun 28, 2017 at 18:06
  • \$\begingroup\$ I see, edit your question with the code that loads the Menu scene \$\endgroup\$ Jun 28, 2017 at 18:10
  • \$\begingroup\$ The code that loads the menu scene is the MenuScript which I put on the question. \$\endgroup\$
    – Haileapp
    Jun 28, 2017 at 18:16
  • \$\begingroup\$ That is the Code that Loads level1, i wanna see the code that Loads the menu \$\endgroup\$ Jun 28, 2017 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .