Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 siberman · Oct 30, 2011 at 11:39 AM · instantiateaudiofade

Fading Audio in/out with object creation

Hello,

I'm trying, as the title strongly suggests, to fade audio in and out based in the creation of an object. I've got the fading down and working when attached to a couple of keys, but when i try calling the function based on object tags i get some odd results. I think it's to do with the update constantly resetting the fade, because it sounds glitchy, but i'm just stumped as to what to do now, i've been churning my noob brain around for hours.

I'd love some help.

Thanks.

     function Update () {
 
 var proxies = GameObject.FindGameObjectsWithTag("Proxy").Length;
 
 
     if (proxies > 0){
 
     FadeInSound();
     
 }
 
     if (proxies <= 0){
 
     FadeOutSound();
     }    
 }
 
         
 var menuMusic:GameObject;
 
 
 function FadeOutSound(){
    
     if (menuMusic) {
        
         for (var i = 99; i > 0; i--){
           menuMusic.audio.volume = i * .01;
           yield new WaitForSeconds (.015);
           print ("Fading Out...");
         }
         menuMusic.audio.volume = 0;
     }
 }
 
 function FadeInSound(){
    
     if (menuMusic) {
        
         for (var i = 1; i < 99; i++){
           menuMusic.audio.volume = i * .01;
           yield new WaitForSeconds (.015);
           print ("Fading In...");
         }
         menuMusic.audio.volume = 1;
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Oct 30, 2011 at 03:47 PM

This problem probably is caused by a coroutine being called multiple times. When you call a coroutine (any routine containing yield) it creates an instance which will run in the background until it ends. If you call this coroutine again, another instance will be created, and in the code above the new instance will "fight" with the others already running for the volume control.
You could improve your code and solve this problem using this:

private var proxy = false; private var lastProxy = false;

function Update(){

 if (GameObject.FindWithTag("Proxy")){ // if any Proxy object exists...
     proxy = true; // set proxy flag true
 }
 else {
     proxy = false; // else set it false
 }
 if (proxy != lastProxy){ // if proxy changed state...
     lastProxy = proxy;
     if (proxy){ // and a new Proxy object was found...
         StopCoroutine("FadeOutSound"); // stop FadeOut coroutine (if any)
         FadeInSound(); // and start FadeIn
     } 
     else { // but if no more Proxy objects exist...
         StopCoroutine("FadeInSound"); // stop FadeIn coroutine (if any)
         FadeOutSound(); // and start FadeOut
     }
 }

}

function FadeOutSound(){

 if (menuMusic) {
     for (var i = 99; i > 0; i--){
       menuMusic.audio.volume = i * .01;
       yield new WaitForSeconds (.015);
       print ("Fading Out...");
     }
     menuMusic.audio.volume = 0;
 }

}

function FadeInSound(){

 if (menuMusic) {
     for (var i = 1; i < 99; i++){
       menuMusic.audio.volume = i * .01;
       yield new WaitForSeconds (.015);
       print ("Fading In...");
     }
     menuMusic.audio.volume = 1;
 }

}

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 siberman · Nov 02, 2011 at 06:57 AM 0
Share

Hey, finally had a chance to get back to this, works beautifully, thanks heaps.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Audio fade out problem 1 Answer

audio source on instantiating game object 0 Answers

Audio crossfade on single key press 1 Answer

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

Fade Audio 1 Answer


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