Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[C#]Audio not stopping when new scene is entered(Please Help)

Discussion in 'Scripting' started by KyleStank, May 15, 2014.

  1. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    I am trying to add the last thing to game...audio. I menus in my game and I want the music to stop playing when you enter one of the levels. This script is not quite complete yet but I do not want to continue until I can stop playing audio when the player wnters a new level. The problem is I am using DontDestroyOnLoad(gameObject) and this is not destroying the object. Even when I try Destroy(gameObject) the object still does not go away. I am a noob for putting sound into my games. This is the VERY first time I have tried adding sound to my games. This is also the first game I have made. Everything is done EXCEPT audio! Please help! Here is my script: (note that some of this code is weird for debugging reasons)
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MenuBackgroundMusic : MonoBehaviour
    5. {
    6.     private static MenuBackgroundMusic backgroundMusic = null;
    7.     public bool menuBackgroundMusicPlay = false;
    8.     public bool levelBackgroundMusicPlay = false;
    9.  
    10.     void Awake ()
    11.     {
    12.         if (Application.loadedLevelName == "Main Menu" || Application.loadedLevelName == "Level Selecter" || Application.loadedLevelName == "Game Shop")
    13.         {
    14.             audio.Play();
    15.             audio.loop = true;
    16.             menuBackgroundMusicPlay = true;
    17.         }
    18.         if (Application.loadedLevelName == "Level1")
    19.         {
    20.             menuBackgroundMusicPlay = false;
    21.         }
    22.         if (menuBackgroundMusicPlay == true)
    23.         {
    24.             DontDestroyOnLoad(gameObject);
    25.         }
    26.     }
    27.  
    28.     void Update ()
    29.     {
    30.         if (menuBackgroundMusicPlay == false)
    31.         {
    32.             if (Time.time >= 10.0f)
    33.             {
    34.                 audio.Stop();
    35.                 audio.loop = false;
    36.             }
    37.         }
    38.     }
    39. }
    40.  
     
  2. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
  3. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    I was doing a singleton. I will try that method and see if it works. I may not to get back to you in a while though because I am going away for a few days.
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    We have two services we use, an AudioManager and a MusicManager. This lets us control the volume of sound effects and music separately.

    One of the benefits of our MusicManager is that it can only play one audio clip at a time. So if we have a Start screen playing one song, the song will continue to play through level loading until the next scene sends the command to play it's own music.
     
  5. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    Okay but how do I use MusicManger? I tried to add it as a class in my script and add it as a component for my game object and nothing popped up for either one.
     
  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I'm not sure what MusicManager you are using. The one we have was made in house by us. Maybe I'll write my own and release it open source sometime.

    This should live on a MonoBehaviour that doesn't get destroyed ever. In fact, our MusicManager is usually a singleton or a service so only one exists ever, guaranteed.

    EDIT: Went ahead and completed it so it's a singleton. NOT tested.
    EDIT2: Bug fixes. Still not tested.
    Code (csharp):
    1.  
    2. public class MusicSingleton : MonoBehaviour {
    3.  
    4.   public static MusicSingleton instance { get; private set; }
    5.  
    6.   void Awake() {
    7.     if (instance == null) {
    8.       instance = this;
    9.       DontDestroyOnLoad(gameObject);
    10.     }
    11.     else { Destroy(gameObject); }
    12.   }
    13.  
    14.   /// Stops previous music and plays music clipToPlay
    15.   public void PlayMusic(AudioClip clipToPlay) {
    16.     if (audioSource != null  audioSource.clip != clipToPlay) {
    17.       audioSource.Stop();
    18.       Destroy(audioSource);
    19.     }
    20.    
    21.     if (audioSource == null) {
    22.       audioSource = gameObject.AddComponent<AudioSource>();
    23.       audioSource.clip = clipToPlay;
    24.       audioSource.loop = true;
    25.       audioSource.Play();
    26.     }
    27.  
    28.   AudioSource audioSource { get; set; }
    29.  
    30. }
    31.  
    After putting this in your first scene, you should be able to play music with this command
    Code (csharp):
    1.  
    2. // Stops previous music and plays new music
    3. MusicSingleton.instance.Play(whateverAudioClip);
    4.  
     
    Last edited: May 17, 2014
  7. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    Okay, like all this has errors because it says that the variable audioSource does not exist in the context and I created a script just for this coding using the same class name as well. The "AudioSource audioSoruce {get; set;}" also gives me an error that says the keyword "get;" does not exist in the current context. With these errors, all the times that you mentioned the variable "audioSource", there is an error because the variables does not exists because when you are defining the variables, there is an error in it so it never really gets created. I know NOTHING about this. Shoot, I never heard of a MusicManager until you posted something about one. So how could I fix this? And by the way, I am no Unity3D pro. But I can say I have a little more experience then what I did 3 months ago.(or way more actually) Thank you!