Audiomanager, FadeIn, FadeOut question

Hello I was wondering if anyone here knows a way to implement a fade in, fade out function to this script?
I want to fade out the music and the forest ambience after a certain time and then I want to fade in another sound but I can’t seem to get it to work

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Sound
{
    public string name;
    public AudioClip clip;

    [Range(0f,1f)] // Attribute, slider with Min & Max
    public float volume = .7f;
    [Range(1f,2.2f)]
    public float pitch = 2f;

    [Range(0f,.2f)]
    public float randomVolume = 0.1f;
    [Range(0f,.2f)]
    public float randomPitch = 0.1f;

    public bool loop = false;

    private AudioSource source;

    public void SetSource(AudioSource _source)
    {
        source = _source;
        source.clip = clip;
        source.loop = loop;
    }

    public void Play()
    {
        source.volume = volume * (1 + Random.Range(-randomVolume/2f, randomVolume/2f));
        source.pitch = pitch * (1 + Random.Range (-randomPitch / 2f, randomPitch / 2f));
        source.Play ();
    }

    public void Stop()
    {
        source.Stop ();
    }

    public void Pause()
    {
        source.Pause ();
    }
}

public class AudioManager : MonoBehaviour
{
    public static AudioManager instance;

    [SerializeField]
    Sound[] sounds;

    void Awake()
    {
        if (instance != null)
        {
            if (instance != this)
            {
                Destroy (this.gameObject);
            }
        }
        else
        {
            instance = this;
            DontDestroyOnLoad (this);
        }
    }

    void Start()
    {
        for (int i = 0; i < sounds.Length; i++)
        {
            GameObject _go = new GameObject ("Sound_" + i + "_" + sounds[i].name);
            _go.transform.SetParent (this.transform);
            sounds [i].SetSource (_go.AddComponent<AudioSource> ());
        }
        PlaySound ("Music");
        PlaySound ("ForestAmbience");
    }

    public void PlaySound(string _name)
    {
        for (int i = 0; i < sounds.Length; i++)
        {
            if (sounds [i].name == _name)
            {
                sounds [i].Play ();
                return;
            }
        }

        // No sound with _name
        Debug.LogWarning ("AudioManager: Sound not found in list, " + _name);
    }

    public void StopSound(string _name)
    {
        for (int i = 0; i < sounds.Length; i++)
        {
            if (sounds [i].name == _name)
            {
                sounds [i].Stop ();
                return;
            }
        }

        // No sound with _name
        Debug.LogWarning ("AudioManager: Sound not found in list, " + _name);
    }

    public void PauseSound(string _name)
    {
        for (int i = 0; i < sounds.Length; i++)
        {
            if (sounds [i].name == _name)
            {
                sounds [i].Pause ();
                return;
            }
        }
    }
}

No one? I’m open to suggestions

@sigl3793 - Hi!

I’m not able to give ready to use code for your case, but I think this could help
(didn’t read it, just took a quick look at it):

  1. Define variable for seconds left
  2. Get current Audioclip length
  3. Monitor seconds left for current audioClip, when there is only that amount of time left:
    2.1 Start coroutine to fade out current clip
    2.2 Start coroutine to fade in new clip
  4. Coroutine will fade current clip out and at the same time, another coroutine will ramp new audio up.

IIRC, that’s how I solved similar fade in/out for background music…