Cycle things with one key

Hi!
I want to cycle music with same key (in my case, letter “m”) but I don’t know how to write the function in.

Now I can crossfade music with two different keys but that is not I want. What should I do?

Help would be much appreciated :slight_smile:
Here is the code:

using UnityEngine;
using System.Collections;

public class scr_MusicController : MonoBehaviour
{

    void Update()
    {
        if (Input.GetKeyDown("m"))
        {
            StartCoroutine("ChangeMusic1");
        }
        if (Input.GetKeyDown("n"))
        {
            StartCoroutine("ChangeMusic2");
        }
    }
    private IEnumerator ChangeMusic1()
    {
        float fTimeCounter = 0f;

        while (!(Mathf.Approximately(fTimeCounter, 1f)))
        {
            fTimeCounter = Mathf.Clamp01(fTimeCounter + Time.deltaTime);
            transform.FindChild("aud_Normal").audio.volume = 1f - fTimeCounter;
            transform.FindChild("aud_Retro").audio.volume = fTimeCounter;
            yield return new WaitForSeconds(0.02f);
        }

        StopCoroutine("ChangeMusic1");
    }
    private IEnumerator ChangeMusic2()
    {
        float fTimeCounter = 0f;

        while (!(Mathf.Approximately(fTimeCounter, 1f)))
        {
            fTimeCounter = Mathf.Clamp01(fTimeCounter + Time.deltaTime);
            transform.FindChild("aud_Retro").audio.volume = 1f - fTimeCounter;
            transform.FindChild("aud_Normal").audio.volume = fTimeCounter;
            yield return new WaitForSeconds(0.02f);
        }

        StopCoroutine("ChangeMusic2");
    }
}

public class scr_MusicController : MonoBehaviour
{
public string sourceNames = new string { “aud_Normal”, “aud_Retro”};
private AudioSource audioSources;
private int currentSound;

   void Start()
   {
       // Set up the list of audioSources to be switched 		
       if ( sourceNames != null && sourceNames.Length > 0 )
       {
   	   audioSources = new AudioSource[sourceNames.Length];
   	   for(int i=0; i<sourceNames.Length; i++)
          {
            audioSources _= transform.FindChild(sourceNames*).audio;*_

* }*
currentSound = 0;
* enabled = true; *
}
else
{
Debug.LogError(“No AudioSource names set - disabling script”);
* enabled = false; *
*} *
}

void Update()
{
if (Input.GetKeyDown(“m”) )
*{ *
StopCoroutine(“ChangeMusic”);
StartCoroutine(“ChangeMusic”);
}
*} *
private IEnumerator ChangeMusic()
{
float fTimeCounter = 0f;
int oldSound = currentSound;
int nextSound = (currentSound + 1) % audioSources.Length;

currentSound = nextSound;

while (!(Mathf.Approximately(fTimeCounter,1f)))
{
fTimeCounter = Mathf.Clamp01(fTimeCounter + Time.deltaTime);
// I fade off any sound which is not our target sound (the “nextSound”)
// since you might be cycling a sound while the last one or more crossfades
// were not done yet
* for(int i=0; i<audioSources.Length; i++) *
{
* if ( i != nextSound )*
_ audioSources*.volume = 1f - fTimeCounter;
else*
audioSources*.volume = fTimeCounter;
}*_

yield return new WaitForSeconds(0.02f);
}

* // You do not need to stop a coroutine at the end of its scope - the end of the*
// scope means the coroutine is about to stop processing. You only use
// StopCoroutine when you want it to stop in the middle or processing
}
}