Search Unity

Question Background Music not starting and not fading

Discussion in '2D' started by Proxima_Centauri, Oct 27, 2020.

  1. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    I'm adding Background music to my game, and I'm trying to implement a system to change the music when I enter a different area. For that, I created an Audio manager and attached it to my Finish trigger (the Scene manager) I did it following this Brackeys tutorial:


    Then I added to my script in the finish trigger two public bools (enterMusic and exitMusic, so I can check them in the Inspector in each Scene to determine if music has to start when loading the scene or stop when exiting the scene) and two public strings (oldMusic, the music that will stop playing and newMusic, the musiv that will start playing).

    I check in the Awake method if enterMusic is activated, and if it is, it'll play the newMusic corresponding to the name, and I do the same with the exitMusic stopping the oldMusic with an onCollisionEnter2D method (when the player touches the goal). Here's the code I'm using:

    Code (CSharp):
    1. void Awake()
    2.     {    
    3.         if (enterMusic)
    4.         {
    5.             FindObjectOfType<AudioManager>().Play(newMusic);
    6.         }
    7.     }
    8.  
    9.     private void OnTriggerEnter2D(Collider2D col)
    10.     {
    11.         if (col.gameObject.tag == "Player")
    12.         {
    13.             if (exitMusic)
    14.             {
    15.                 FindObjectOfType<AudioManager>().Stop(oldMusic);
    16.             }
    17. //This is to play a transition while changing scenes, not relevant to the audio
    18.             StartCoroutine(TransitionToNextLevel());  
    19.         }    
    20.     }

    That Play and Stop are methods inside the Audio Manager, here they are:


    Code (CSharp):
    1. public void Play(string name)
    2.     {
    3.         AudioList s = Array.Find(sounds, AudioList => AudioList.name == name);
    4.         if (s == null)
    5.         {
    6.             Debug.LogWarning("Sound ''" + name + "'' not found!");
    7.             return;
    8.         }
    9.         s.source.Play();
    10.     }
    11.  
    12.     public void Stop(string name)
    13.     {
    14.         AudioList s = Array.Find(sounds, AudioList => AudioList.name == name);
    15.         if (s == null)
    16.         {
    17.             Debug.LogWarning("Sound ''" + name + "'' not found!");
    18.             return;
    19.         }    
    20.         s.source.Stop();
    21.     }
    It works, except for two problems:

    1. Even though I check the newMusic on my initial scene (so it should start playing music right away), the music is not played, but the command technically works, because when I change scene, the newMusic is read and it happens correctly, it only doesn't play when I firstly start my game.

    2. I want the music to firstly lower its volume until it's mute, then stop it, to make a nice Fade Out effect instead of stopping the musing abruptly. I tried using a Coroutine like this:

    Code (CSharp):
    1. IEnumerator FadeOutMusic(AudioList s)
    2.     {
    3.         while (s.volume > 0f)
    4.         {
    5.             s.volume *= 0.95f;
    6.             yield return null;
    7.         }
    8.         s.volume = 0f;
    9.         yield return null;
    10.     }    
    and called it like this:

    Code (CSharp):
    1. public void Stop(string name)
    2.     {
    3.         AudioList s = Array.Find(sounds, AudioList => AudioList.name == name);
    4.         if (s == null)
    5.         {
    6.             Debug.LogWarning("Sound ''" + name + "'' not found!");
    7.             return;
    8.         }
    9.         StartCoroutine(FadeOutMusic(s));
    10.         s.source.Stop();
    11.     }
    But that Coroutine is not working, the sound still stops abruptly, is there any mistake in the coroutine?
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,466
    This link may help with fading in and out of music:

    https://gamedevbeginner.com/how-to-fade-audio-in-unity-i-tested-every-method-this-ones-the-best/

    I am not entirely certain for your first issue with it not playing immediately, but after a glance at your code, maybe its because you mention you toggle the newMusic once you run it, but you are checking if enterMusic is true on Awake, which only happens right when you load. So, if you are hitting play and then toggling it to true, it shouldnt find that AudioManager and play until you load a new scene. But, if you are marking it true and then hitting play, it should work. Maybe i am misunderstanding but that is my first take on your issue.