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 rjr · Aug 31, 2016 at 10:50 AM · coroutinecoroutinescoroutine errors

Multiple coroutine exicution order

Hello
I have written a coroutine for fad in and fade out the sound effect

private IEnumerator FadeInOut(AudioSource source, float toVal, bool shouldStop, float lerpDuration = 5) { %|-237000201_2|% %|32912738_3|% %|-1584710926_4|% %|2065127481_5|% %|83609385_6|% // lerp logic for executing in a given time. %|1478197646_7|% %|-880432233_8|% %|-1229117739_9|% %|277229335_10|% %|1474934169_11|% %|1679487772_12|% %|1387183729_13|% %|-75361698_14|% %|2100960351_15|% %|-1328175113_16|% %|-1906317594_17|% %|-1665580146_18|% To Fade In and Out the sound i have this methods

 private void FadeInMusic(AudioSource sourceToFadeIn)
 {

// this will fade in the volume source.volume to 1 (Normally 0 to 1) StartCoroutine (FadeInOut (sourceToFadeIn,1,false,m_fadeDuration)); }

private void FadeOutMusic(AudioSource sourceToFadeOut) { // this will fade in the volume source.volume to 0 (Normally 1 to 0) StartCoroutine (FadeInOut (sourceToFadeOut,0,true,m_fadeDuration)); }

`

if i call FadeInMusic() and FadeOutMusic() back to back (or with a very small delay) by passing the same Audio source, two instances of FadeInOut coroutine starts to execute
since FadeInMusic() is called first it will finish execution before FadeOutMusic() Problem i am facing here is in unity is Volume is taking the value from FadeInMusic() function call coroutine, once it is done executing then it will take the value from FadeOutMusic() function call coroutine.

What i have to do to change the coroutine execution order or overriding volume value from 2nd coroutin ? ? As i want the value from FadeOutMusic() function call coroutine.

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
0
Best Answer

Answer by rockin · Aug 31, 2016 at 11:36 AM

You can create a private variable to hold your state:

 private int step = 0;
 
 IEnumerator Coroutine1(){
     if (step == 0) {
          // Do stuff.
         step++;
     }
 }
 
 IEnumerator Coroutine2(){
     if (step == 1) {
          // Do stuff.
         step++;
     }
 }
 
 ....
 
 IEnumerator CoroutineLast(){
     if (step == 1) {
          // Do stuff.
         step = 0;
     }
 }

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 rjr · Sep 07, 2016 at 05:37 AM 0
Share

Hello rockin, thanks for the reply.

Could you please explain in detail.

As i need the unity to update the value from last coroutine called, but unity is considering the value of first coroutine called.

from your above example lets take coroutine1 and coroutine2, called on the same audio source. since the coroutine2 is called last i need unity to consider only this value and apply to audio source, but unity still stick on coroutine1 and update audio source by coroutine1 value, once coroutine1 finish executing then it will start taking leftover value from coroutine2.

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

56 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

Related Questions

How to stop a coroutine started in Script A from within Script B 1 Answer

Coroutines not passing yield 1 Answer

Collection Change During Iteration 0 Answers

Why doesn't a coroutine start executing from beginning when I start it again? 1 Answer

Coroutine Won't Stop Using IEnumerator 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