- Home /
The question is answered, right answer was accepted
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;
}
}
This has been asked and answered multiple times. Typing "how to fade out audio java" into the search bar above you would find numerous posts about that.
Unity Answers is not a forum for "How to do this" questions. Vist Unity Forums for that. http://forum.unity3d.com/
Please read the FAQ carefully before posting questions or comments in the future. http://answers.unity3d.com/page/faq.html
Don't just ask a new question every time you run into a problem. Search for it first.
Type "unity how to fade out audio javascript" into google.
Click the link: Help with Audio Fade - Unity Answers
The accepted answer there contains code:
function Update(){ if (Input.Get$$anonymous$$eyDown("F")){ FadeOutSound(); } } var menu$$anonymous$$usic:GameObject; function FadeOutSound(){ if (!menu$$anonymous$$usic){ // Get menu$$anonymous$$usic if not already defined in Inspector menu$$anonymous$$usic = GameObject.Find("$$anonymous$$enu$$anonymous$$usic"); } if (menu$$anonymous$$usic) { // wait 4 seconds then fades for 4.5s yield WaitForSeconds (4); for (var i = 9; i > 0; i--){ menu$$anonymous$$usic.audio.volume = i * .1; yield new WaitForSeconds (.5); print ("Fading..."); } menu$$anonymous$$usic.audio.volume = 0; } }
Copy it into Unity.
Unity says that scripts contain obsolete API's. It will adapt those automatically if you click yes.
Open script. It adapted the GetComponent() command, which you already use in your script.
function Update(){ if (Input.Get$$anonymous$$eyDown("F")){ FadeOutSound(); } } var menu$$anonymous$$usic:GameObject; function FadeOutSound(){ if (!menu$$anonymous$$usic){ // Get menu$$anonymous$$usic if not already defined in Inspector menu$$anonymous$$usic = GameObject.Find("$$anonymous$$enu$$anonymous$$usic"); } if (menu$$anonymous$$usic) { // wait 4 seconds then fades for 4.5s yield WaitForSeconds (4); for (var i = 9; i > 0; i--){ menu$$anonymous$$usic.GetComponent.<AudioSource>().volume = i * .1; yield new WaitForSeconds (.5); print ("Fading..."); } menu$$anonymous$$usic.GetComponent.<AudioSource>().volume = 0; } }
Drag an AudioSource onto "menu$$anonymous$$usic" and run scene.
$$anonymous$$ake sure the audio plays.
Press F.
Unity says: ArgumentException: Input $$anonymous$$ey named: F is unknownLook at script. Change the line using
$$anonymous$$eyCode.F
ins$$anonymous$$d of"F"
.If for some reason you don't know about $$anonymous$$eyCodes, you can always google the error and find the information within 2 seconds.Save script. Run the scene. Press F. Look at the Inspector. It fades to 0.
It took me 10 $$anonymous$$utes to:
google "unity how to fade out audio javascript", read a bit of the surrounding texts.
copy the code from there to my scene, convert it and try it with an audiosource.
google "ArgumentException: Input $$anonymous$$ey named: F is unknown".
You spent "hours" searching, but couldn't just try what others said, even if it was years ago.
You should have a look at this. It is also featured in the FAQ:
http://mattgemmell.com/what-have-you-tried/
$$anonymous$$aybe this text helps you.
Answer by RandomCharacters · Nov 18, 2015 at 02:55 AM
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;
}
}
///////////////////////////////////////////////////////
Follow this Question
Related Questions
Fade audio in and out for Jet Engine 1 Answer
Fade in AudioLister over time #c 1 Answer
how to fade audio if Time.timeScale = 0.0 ?? 2 Answers
Fade In/Out Audio After Enter Trigger 3 Answers