Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Custard · Jun 30, 2011 at 05:32 PM · audiofade

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");
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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;
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Custard · Jul 01, 2011 at 06:28 PM 0
Share

Thanks very much indeed, works exactly as intended.

avatar image siberman · Oct 30, 2011 at 10:32 AM 0
Share

Never$$anonymous$$d, i figured it out, cheers.

avatar image
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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Custard · Jul 01, 2011 at 09:00 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges