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 /
This question was closed Nov 19, 2015 at 12:46 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
-1
Question by RandomCharacters · Nov 17, 2015 at 11:08 PM · audiocoroutinefade

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;
     }
 }


Comment
Add comment · Show 2
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 Le-Pampelmuse · Nov 17, 2015 at 11:30 PM 2
Share

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.

avatar image Le-Pampelmuse · Nov 18, 2015 at 02:10 AM 0
Share
  1. Type "unity how to fade out audio javascript" into google.

  2. Click the link: Help with Audio Fade - Unity Answers

  3. 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;
          }
      }
    
  4. Copy it into Unity.

  5. Unity says that scripts contain obsolete API's. It will adapt those automatically if you click yes.

  6. 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;
          }
      }
    
  7. 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 unknown

  8. Look 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.

  9. Save script. Run the scene. Press F. Look at the Inspector. It fades to 0.

It took me 10 $$anonymous$$utes to:

  1. google "unity how to fade out audio javascript", read a bit of the surrounding texts.

  2. copy the code from there to my scene, convert it and try it with an audiosource.

  3. 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.

1 Reply

  • Sort: 
avatar image
0
Best Answer

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;
         }
 }
 ///////////////////////////////////////////////////////
Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to fade in-out groups of audio sources? 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