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 Syntronis · Jul 25, 2017 at 02:51 PM · soundfadeoutfadein

Audio Fade Trigger Script - Coroutine Bug

Hello. I'm building a system of trigger with a script that have 2 behaviours:

  1. OnTriggerEnter: the audio-clip (music) plays, and the volume increase from 0 to 1 in 2 seconds.

  2. OnTriggerExit: the audio-clip decrease the volume from 1 to 0 in 2 seconds, then pause.

Everything works just fine, until i "break the system" by doing 2 things

  1. I exit the trigger before the volume arrived to 1 (right after entering)

  2. I enter the trigger before the volume arrived to 0 (right after exiting)

I'm sure this is because the Coroutine is broken on midle of operation. What happend is that the volume stay stuck at the level it was when i entered / exited the trigger while the coroutine is playing the precedent fade effect.

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class VolumeFadeTrigger : MonoBehaviour
 {
     public AudioSource source;
     public AudioClip clip;
 
     public void Awake()
     {
         source = GetComponent<AudioSource> ();
     }
 
 
     public void OnTriggerEnter(Collider other)
     {
         StartCoroutine ("FadeIn");
     }
 
 
     public void OnTriggerExit(Collider other)
     {
         StartCoroutine ("FadeOut");
     }
 
 
     IEnumerator FadeOut()
     {
         while (source.volume > 0.01f) 
         {
             source.volume -= Time.deltaTime / 2.0f;
             yield return null;
         }
         source.volume = 0;
         source.Pause();
     }
 
 
     IEnumerator FadeIn()
     {
         source.Play ();
         while (source.volume < 0.99f) 
         {
             source.volume += Time.deltaTime / 2.0f;
             yield return null;
         }
         source.volume = 1;
     }
 }
 

Any idea how i can fix this ? (if the volume is actually fading in, and i exit the trigger before it ends, it will just take it from there and fade out naturally.)

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 Syntronis · Jul 26, 2017 at 08:43 PM 0
Share

I still didn't find any way to resolve the issue, and see my question already lost under hundreds of other questions, so i'm really sorry to post again for up.

avatar image linelade Syntronis · Aug 25, 2017 at 10:31 AM 0
Share

hey @Syntronis

did you find a solution yet? im looking for exact same thing!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Aug 25, 2017 at 10:48 AM

The problem is that your two coroutines work with the same variable, in your case with source.volume. While one coroutine decrements the value, the other increments the value. So you never reach any of the desired values.

There are many ways how you can solve this. The most easiest way is to use a local variable instead

 for(float v = source.volume; v > 0f; v -=Time.deltaTime / 2.0f) 
 {
     source.volume = v;
     yield return null;
 }

If you design your two coroutines like this each will be ensured to finish as they work with a local variable and no other code can change the local variable. Though if two coroutines run at the same time the actual volume value would bounce between the current values of those coroutines.

A better way would be to simply stop the opposite coroutine when you start a new one. Since you use the string version of StartCoroutine you can simply use StopCoroutine("FadeOut") when you start FadeIn and the other way round.

When not using the string version you can store the current "Coroutine" object in a variable. It can be used to stop the old coroutine. This is probably the best solution:

 Coroutine current = null;
 
 public void OnTriggerEnter(Collider other)
 {
     if (current != null)
         StopCoroutine(current);
     current = StartCoroutine (FadeIn());
 }
 
 public void OnTriggerExit(Collider other)
 {
     if (current != null)
         StopCoroutine(current);
     current = StartCoroutine (FadeOut());
 }
 
 IEnumerator FadeOut()
 {
     for (float v = source.volume; v > 0f; v -= Time.deltaTime / 2.0f) 
     {
         source.volume = v;
         yield return null;
     }
     source.volume = 0;
     source.Pause();
     current = null;
 }
 
 IEnumerator FadeIn()
 {
     source.Play ();
     for (float v = source.volume; v < 1f; v += Time.deltaTime / 2.0f) 
     {
         source.volume = v;
         yield return null;
     }
     source.volume = 1f;
     current = null;
 }

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

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

70 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 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

Stop BGM while movie is playing. 0 Answers

Making Enemy Audio quieter 1 Answer

Fade in/out a GameObject 1 Answer

Multiple Splash Screens for a game intro? 2 Answers

Fading from my splash screen 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