title: Audio Fade In/Out with C# in Unity?
body: <p>I just added an audio manager to my android platformer game in Unity and the manager is based on this <a href="https://www.youtube.com/watch?v=6OT43pvUyfY" rel="nofollow noreferrer">video tutorial from Brackeys</a> and it is working as expected. I would like to ask someone to help me to understand how can I implement an Audio Fade In/Out code (I think it will be a coroutine) so when the game starts all sounds fade in and when I call a function on a button (an Exit game UI button is already created) than all sounds fade out. </p>
  
  <p>I tried to implement a coroutine from my previous UI Manager script but unfortunately somehow I'm having problems to understand the problem. Every other functionalities of the game are working as I wanted. </p>
  
  <p>Music Class.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>using UnityEngine.Audio;
  using UnityEngine;
  
  [System.Serializable]
  public class Classmusic
  {
      public string name;
  
      public AudioClip clip;
  
      [Range(0f, 1f)]
      public float volume;
      [Range(0.1f, 3f)]
      public float pitch;
  
      public bool loop;
  
      [HideInInspector]
      public AudioSource source;
  }
  
  </code></pre>
  
  <p>Music Player.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.Collections;
  using UnityEngine;
  using System;
  using UnityEngine.Audio;
  
  
  public class MPlayer : MonoBehaviour
  {
      public Classmusic[] music;
  
      private void Awake()
      {
          foreach (Classmusic m in music)
          {
              m.source = gameObject.AddComponent&lt;AudioSource&gt;();
              m.source.clip = m.clip;
              m.source.volume = m.volume;
              m.source.pitch = m.pitch;
              m.source.loop = m.loop;
          }
      }
  
      private void Start()
      {
          Play("MainMusic");
      }
  
      public void Play (string name)
      {
          Classmusic m = Array.Find(music, sound =&gt; sound.name == name);
          if (m == null)
          {
              Debug.LogWarning("Music: " + name + " not found!");
              return;
          }
          m.source.Play();
  
      }
  }
  </code></pre>