- Home /
 
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);
 }
 
              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...
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.
I think your misunderstanding the problem, the game doesn't freeze, it just doesn't continue.
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;
 }
 
                 Answer by LuisGuimaraes · Dec 17, 2013 at 06:43 PM
This is old but:
      //...
      Application.LoadLevel(level);
 }
 
 function OnLevelWasLoaded ()
 {
     //Fade texture out
     //...
 
              Your answer
 
             Follow this Question
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