simple question about coroutine an audio (I want to fade out music in javascript)

So I have a javascript game I am redoing and almost done. In one of the functions I call the coroutine to adjust the audio. It does this ok, but I can’t set the length. How do you do that.

  • So looking at the script, I have a variable at the top 'linkToMusic;. This is set in the inspector to another game object that holds the msic.
  • I then (in a function) call the coroutine ‘FadeAudioOut();’.
  • In the corotuine, I am trying to fade out the music over a 1 second real time period to match up with the video fade out to back.

So basically, it’s just how do I set the time in the coroutine to 1 second fade to 0 or even a 2 second fade to 0?

///////////////////////////////////////////////////////
	var linkToMusic : GameObject;    
///////////////////////////////////////////////////////
//Fade Audio Out
	FadeAudioOut();    
//////////////////////////////////////////////////////
//Audio fade out coroutine 
function FadeAudioOut()
{
	while (linkToMusic.GetComponent.<AudioSource>().volume > 0)
	{
		linkToMusic.GetComponent.<AudioSource>().volume -= Time.deltaTime;
		yield;
	}
}

Actually I did this and it worked fine. Thanks for your help.

		var linkToMusic : GameObject;
		
		enum Fade {In, Out}
		var fadeTime = 1.0;
//////////////////////////////////////////////////////
//Fade Audio Out
FadeAudio(1.0, Fade.Out);

//////////////////////////////////////////////////////
//Audio fade out coroutine
//Call funtion with number seconds for fade plus (Fade.In) or (Fade.Out)
//Such as:  FadeAudio(1.0, Fade.Out);

function FadeAudio(timer : float, fadeType : Fade)
{
	//Fade Audio Out
		var start = fadeType == Fade.In? 0.0 : 0.3;
		var end = fadeType == Fade.In? 0.3 : 0.0;
		var i = 0.0;
		var step = 1.0 / timer;
		
		while (i <= 1.0)
		{
		i += step * Time.deltaTime;
		linkToMusic.GetComponent.<AudioSource>().volume = Mathf.Lerp(start, end, i);
		yield;
		}
}
///////////////////////////////////////////////////////