Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 NIOS · Aug 03, 2015 at 11:53 AM · audiotriggerupdateaudioplay

Quadruple audio fade script not working completely. Shocker.

I am trying to make a script that plays a song while you are inside a box, and reverts back to the original once you leave. To spice it up, I added fadeouts. Here is the script:

      var track1 : AudioClip;
      var track2 : AudioClip;
      
      audio.clip = track1;
      audio.Play();
      
      var audio1Volume : float = 1.0;
      var audio2Volume : float = 0.0;
      var track1Playing : boolean = true;
      var track2Playing : boolean = false;
      var cango : boolean = false;
      var didgo : boolean = false;
      
      function OnTriggerEnter(hit : Collider){
      cango = true;
      Update();
 }
      function OnTriggerExit(hit : Collider){
      didgo = true;
      Update2();
 }
 
      function Update() {
      if (cango == true) {
      
          fadeOut();
      
          if (audio1Volume <= 0.1) {
              if(track2Playing == false)
              {
                track2Playing = true;
                track1Playing = false;
                audio.clip = track2;
                audio.Play();
              }
              
              fadeIn();
          }
      }
 
      }
      
           function Update2() {
      if (didgo == true){
      
          fadeOut2();
          
          if (audio1Volume <= 0.1) {
              if(track1Playing == false)
              {
                track1Playing = true;
                track2Playing = false;
                audio.clip = track1;
                audio.Play();
          
      }
          fadeIn2();
          }
      }
      }
      
      function fadeIn() {
          if (audio2Volume < 1) {
              audio2Volume += 0.6 * Time.deltaTime;
              audio.volume = audio2Volume;
          }
      }
      
           function fadeIn2() {
          if (audio1Volume < 1) {
              audio1Volume += 0.6 * Time.deltaTime;
              audio.volume = audio1Volume;
          }
      }
      
      function fadeOut() {
          if(audio1Volume > 0.1)
          {
              audio1Volume -= 0.6 * Time.deltaTime;
              audio.volume = audio1Volume;
                 if(audio1Volume <= 0.1){
                 audio.clip = track1;
                 audio.Stop();
                 }
          }
      }
      
      function fadeOut2() {
          if(audio2Volume > 0.1)
          {
              audio2Volume -= 0.6 * Time.deltaTime;
              audio.volume = audio2Volume;
                if(audio2Volume >= 0.1){
                audio.clip = track2;
                audio.Stop();
                }
          } 
      }
 

I get no errors or feedback at all, instead, I get annoyed. It works fine at first; it fades out audio1 and fades in audio 2. But the reverse effect does not work. When you leave, it stops the second track, and if you enter again, it plays it very quietly. Track 1 never returns, from what I can tell, anyways.

I was hoping someone could give me a fresh set of professional eyes to see what I might have done wrong. Thanks.

Comment
Add comment · Show 1
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 tanoshimi · Aug 03, 2015 at 12:01 PM 0
Share

Don't name your functions Update(). Doing so will make them be called as part of Unity's game loop. If that's what you intended, then you shouldn't be calling Update manually from OnTriggerEnter.

Also, your code has an awful lot of duplication. I recommend you not only rename Update/2() and fadeout/2() but also refactor them to be parameterised functions.

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Problem with playing sound on Trigger-Enter 2 Answers

How to create an audio manager script with input from triggers? 2 Answers

MissingMethodException: 'UnityEngine.AudioClip.Play' 2 Answers

Sound On Collision Not Working 3 Answers

Play local Android/IOS audio files in unity game. 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