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 3 · Aug 23, 2013 at 07:12 AM · audioloadloadingfadevolume

Fading out before load script not functioning correctly?

So here is a script i found on the unity forums, and I edited it slightly so it would fade out automatically when loading a level. The script works itself works, but the (if loading level) part i added doesnt work, and now it wont work at all. The objects are not present for some reason when the game loads either. Not sure why, any ideas? Thanks.

 enum Fade {In, Out}
     
     var fadeTime = 3;
     
      
     function Awake () {
             DontDestroyOnLoad (transform.gameObject);
         }
     function Update () {
     if (Application.isLoadingLevel){
         FadeAudio(fadeTime, Fade.Out);
     Destroy (gameObject, 4);
     }
     }
     
      
     
     function FadeAudio (timer : float, fadeType : Fade) {
     
         var start = fadeType == Fade.In? 0.0 : 1.0;
     
         var end = fadeType == Fade.In? 1.0 : 0.0;
     
         var i = 0.0;
     
         var step = 1.0/timer;
     
      
     
         while (i <= 1.0) {
     
             i += step * Time.deltaTime;
     
             audio.volume = Mathf.Lerp(start, end, i);
     
             yield;
     
         }
     
     }
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 sooncat · Aug 23, 2013 at 08:46 AM 0
Share

var fadeTime always the same value?

may be you lost some thing like "fadeTime++".

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Sajidfarooq · Aug 23, 2013 at 07:45 AM

The problem is, since your code is in "Update", the FadeAudio function is called every frame that Application.isLoadingLevel == true . i.e, its called many times over, whereas you should be calling it only once.

The solution is to either call this function in "Start", so its called only once, or to manually create a flag that makes sure that FadeAudio is only called once.

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 3 · Aug 23, 2013 at 08:37 AM 0
Share

this doesn't seem to change anything, any other ideas?

Thanks,

avatar image
0

Answer by 3 · Aug 23, 2013 at 08:41 AM

Solved my own problem, the object was a child object, so the whole thing got deleted. Thanks,

Comment
Add comment · Show 4 · 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 Sajidfarooq · Aug 23, 2013 at 08:54 AM 0
Share

Is the code still in update?

avatar image 3 · Aug 23, 2013 at 11:29 AM 0
Share

Yes it is:

 enum Fade {In, Out}
 
 var fadeTime = 2;
 
  
 function Start () {
 //DontDestroyOnLoad(this.gameObject);
         DontDestroyOnLoad (transform.gameObject);
         if (Application.isLoadingLevel == true){
     FadeAudio(fadeTime, Fade.Out);
 Destroy (gameObject, 3);
 }
     }
 function Update () {
 
 }
 
  function OnLevelWasLoaded (level : int) {
         if (Application.isLoadingLevel == true){
     FadeAudio(fadeTime, Fade.Out);
 Destroy (gameObject, 3);
 }
     }
 
 function FadeAudio (timer : float, fadeType : Fade) {
 
     var start = fadeType == Fade.In? 0.0 : 1.0;
 
     var end = fadeType == Fade.In? 1.0 : 0.0;
 
     var i = 0.0;
 
     var step = 1.0/timer;
 
  
 
     while (i <= 1.0) {
 
         i += step * Time.deltaTime;
 
         audio.volume = $$anonymous$$athf.Lerp(start, end, i);
 
         yield;
 
     }
 
 }
avatar image Sajidfarooq · Aug 23, 2013 at 01:14 PM 0
Share

Looking at your code, no its not. Your Update function is empty! You moved your code to "Start" as I recommended below. Unless you posted the wrong code here...

So your problem was, indeed, that your code was in Update, as I suggested. You can confirm this by returning the code to Update, as you previously had it.

avatar image 3 · Aug 24, 2013 at 03:12 AM 1
Share

Oh your right, I hadn't even noticed. It was a combo of both issues. I checked off your answer, thanks for the help!

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

17 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

Related Questions

Static objects increasing load time in Unity 5? 2 Answers

Why is my backtrack's volume getting lower? 0 Answers

[Daydream] Disable default volume menu? 0 Answers

How to load multiple wav files 5 Answers

Muting Audio 2 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