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 KnC_Studios · Aug 20, 2013 at 10:27 PM · javascriptfadeunity 4not-working

Script not working in Unity 4

Hi, I have a death script, and it worked fine in unity 3.5.7. It would slowly fade to white then load the level again then fade out. Now in unity 4 it just instantly goes to white and doesn't load. Help please!

LevelLoadFade.js

 /*
     Usage:
 
     // Load my level    
     LevelLoadFade.FadeAndLoadLevel("mylevel", Color.white, 0.5);
 
     // Reset the current level
     LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 0.5);
 */
 
 static function FadeAndLoadLevel (level, fadeTexture : Texture2D, fadeLength : float)
 {
     if (fadeTexture == null)
         FadeAndLoadLevel(level, Color.white, fadeLength);
 
     var fade = new GameObject ("Fade");
     fade.AddComponent(LevelLoadFade);
     fade.AddComponent(GUITexture);
     fade.transform.position = Vector3 (0.5, 0.5, 1000);
     fade.guiTexture.texture = fadeTexture;
     fade.GetComponent(LevelLoadFade).DoFade(level, fadeLength, false);
 }
 
 static function FadeAndLoadLevel (level, color : Color, fadeLength : float)
 {
     var fadeTexture = new Texture2D (1, 1);
     fadeTexture.SetPixel(0, 0, color);
     fadeTexture.Apply();
     
     var fade = new GameObject ("Fade");
     fade.AddComponent(LevelLoadFade);
     fade.AddComponent(GUITexture);
     fade.transform.position = Vector3 (0.5, 0.5, 1000);
     fade.guiTexture.texture = fadeTexture;
 
     DontDestroyOnLoad(fadeTexture);
     fade.GetComponent(LevelLoadFade).DoFade(level, fadeLength, true);
 }
 
 function DoFade (level, fadeLength : float, destroyTexture : boolean)
 {
     // Dont destroy the fade game object during level load
     DontDestroyOnLoad(gameObject);
 
     // Fadeout to start with
     guiTexture.color.a = 0;
     
     // Fade texture in
     var time = 0.0;
     while (time < fadeLength)
     {
         time += Time.deltaTime;
         guiTexture.color.a = Mathf.InverseLerp(0.0, fadeLength, time);
         yield;
     }
     guiTexture.color.a = 1;
     yield;
 
     // Complete the fade out (Load a level or reset player position)
     Application.LoadLevel(level);
     
     // Fade texture out
     time = 0.0;
     while (time < fadeLength)
     {
         time += Time.deltaTime;
         guiTexture.color.a = Mathf.InverseLerp(fadeLength, 0.0, time);
         yield;
     }
     guiTexture.color.a = 0;
     yield;
 
     Destroy (gameObject);
 
     // If we created the texture from code we used DontDestroyOnLoad,
     // which means we have to clean it up manually to avoid leaks
     if (destroyTexture)
         Destroy (guiTexture.texture);
 }
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

2 Replies

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

Answer by whydoidoit · Aug 20, 2013 at 11:02 PM

I find that Time.deltaTime tends to be huge in Unity 4 on the first frame after a load. I usually just ignore large values of Time.deltaTime and not add them on to my elapsed time counter...

Comment
Add comment · Show 7 · 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 KnC_Studios · Aug 20, 2013 at 11:21 PM 0
Share

What do you suggest I do to correct this?

avatar image yoyo · Aug 21, 2013 at 06:22 AM 1
Share

Try adding another yield at line 50, after setting time to 0. This will hopefully avoid counting the first frame with a large deltaTime. Alternatively, when accumulating the deltaTime values, ignore any that are greater than some threshold, say 0.1.

avatar image KnC_Studios · Aug 21, 2013 at 02:45 PM 0
Share

I think your misunderstanding the problem, the game doesn't freeze, it just doesn't continue.

avatar image KnC_Studios · Aug 21, 2013 at 04:42 PM 0
Share

Bump, help me please!

avatar image whydoidoit · Aug 21, 2013 at 04:44 PM 1
Share

So do what @yoyo said:

 while (time < fadeLength)
 {
       
    time += Time.deltaTime > 0.3f ? 0 : Time.deltaTime;
    guiTexture.color.a = $$anonymous$$athf.InverseLerp(0.0, fadeLength, time);
    yield;
 }
Show more comments
avatar image
0

Answer by LuisGuimaraes · Dec 17, 2013 at 06:43 PM

This is old but:

      //...
      Application.LoadLevel(level);
 }
 
 function OnLevelWasLoaded ()
 {
     //Fade texture out
     //...
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

18 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

Related Questions

Multiple Cars not working 1 Answer

Int and Javascript help 2 Answers

Boxcollider 2D Destroyed 2 Answers

Unexpected char : 0x0 2 Answers

Another Null Reference Exception 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