- Home /
Help with Audio Fade
After spending an embarrassing amount of hours trying to figure out how to fade out an audio clip I finally concede my incompetence. Would a kind soul give a helping hand please?
Simple script I have so far:
function Start() {
// Get the object with the audio attached from main menu
var menuMusic : GameObject = GameObject.Find("MenuMusic");
if (menuMusic) {
// Stop the menu music
yield WaitForSeconds (4);
Destroy(menuMusic);
}
}
The object that has this script attached is not the object that has the audio source attached.
Have tried various scripts from related questions but I obviously dont seem to understand them enough to implement them.
Any help will be appreciated.
EDIT: Latest attempt below, still not working.
function Start() {
// Get the object with the audio attached from main menu
var menuMusic : GameObject = GameObject.Find("MenuMusic");
if (menuMusic) {
// Stop the menu music
yield WaitForSeconds (4);
FadeOutSound();
print ("Get Fade Menu Music Function");
//Destroy(menuMusic);
}
}
function FadeOutSound(){
var i : int;
for (i = 9; i > 0; i--)
{
audio.volume = i * .1;
yield new WaitForSeconds (.5);
print ("Fade Menu Music");
}
}
Answer by aldonaletto · Jul 01, 2011 at 05:56 PM
In your script, fading out would start 4s after the object was created (the object containing the script). I suspect that's not what you wanted, so I separated the fading function and did some modifications: you can drag the object containing the audio to the Inspector var menuMusic; when FadeOutSound is called, it will use this var if you've defined the object, otherwise it will search for a child object named MenuMusic. Anyway, it will wait for 4 seconds, then fades out the music in 4.5s.
You can call this function when a key is pressed, for instance:
function Update(){
if (Input.GetKeyDown("F")){
FadeOutSound();
}
}
var menuMusic:GameObject;
function FadeOutSound(){
if (!menuMusic){
// Get menuMusic if not already defined in Inspector
menuMusic = GameObject.Find("MenuMusic");
}
if (menuMusic) {
// wait 4 seconds then fades for 4.5s
yield WaitForSeconds (4);
for (var i = 9; i > 0; i--){
menuMusic.audio.volume = i * .1;
yield new WaitForSeconds (.5);
print ("Fading...");
}
menuMusic.audio.volume = 0;
}
}
Answer by BinaryCaveman · Jun 30, 2011 at 09:37 PM
Here is a script from the forums: *How To --> Fade Out Music Before Loading Level*
You should be able to implement that pretty easily. If you don't need to load a level with your script, just remove the Application.LoadLevel(loadLevel);
line.
For example:
function Start() {
// Get the object with the audio attached from main menu
var menuMusic : GameObject = GameObject.Find("MenuMusic");
if (menuMusic) {
// Stop the menu music
FadeOutSoundAndLoadLevel(0);
}
}
Just remove the Update()
and OnGUI()
methods.
Thanks for the reply. That was one of the answers I tried previously with no luck. Given it another shot with your instructions and unfortunately still cant pull it off. Not sure if It has something to do with no being able to find way of adding a number to var i: int;, The object with this script attached doesn't show a slot for one in the inspector. I am possibly missing something obvious? Both the print statements I added are visible in the console.
I have updated my script at the top to show what I have now.
Your answer
Follow this Question
Related Questions
Why my sound it's not attaching to my Flashlight 2 Answers
Clicking Game Object Scrpit 2 Answers
Easy fade in/out on level load/end? 2 Answers
Using High Pass Filter with script 2 Answers
How to emit an audioClip on Collision 0 Answers