Screen Fader Problem

I have a problem with a script that should run the Fade In and Fade Out between the start and the end of the scene.
The script is this:

using UnityEngine;
using System.Collections;

public class Fade_Screen_Scene : MonoBehaviour
{
   public float fadeSpeed = 5.0F;   
   public GUITexture blackScreen;
   private bool sceneStarting = true;  

   void Awake()
   {
       blackScreen.pixelInset = new Rect (0f, 0f, Screen.width, Screen.height);
   }


   void Update()
   {
       if (sceneStarting == true)
       { 
       StartScene();
       }
   }


   void FadeToClear()
   {
       blackScreen.color = Color.Lerp(blackScreen.color, Color.clear, fadeSpeed * Time.deltaTime);
   }


   void FadeToBlack()
   {
       blackScreen.color = Color.Lerp(blackScreen.color, Color.black, fadeSpeed * Time.deltaTime);
   }


   void StartScene()
   {
       FadeToClear();
       if (blackScreen.color.a == 0.0f)
       {
           blackScreen.color = Color.clear;
           blackScreen.enabled = false;
           sceneStarting = false;
       }
   }


   public void EndScene()
   {
       blackScreen.enabled = true;
       FadeToBlack();
   }
}

This script is put in a game object with a GUI Texture component

When the scene starts, a custom texture (in this case a black screen) Fade In, from black to clear. But when I press a trigger that enables the end of the scene, and calls the public void EndScene (), the texture DON’T Fade Out.
I don’t understand why and I tried so much. Please help me.

Okay, so you might want to manually center the mouse when the game starts. The Input class has a static function that allows you to set the mouse position.

1 Answer

1

It’s because the EndScene gets called once, does a single step to fade and stops. When you’re doing your StartScene method you support the continous calls each frame in the Update method that will show each step during a frame because of the method in the Update. Once faded(alpha set to 0) you’re setting sceneStarting to false which will stop the StartScene method from being called in the Update Method.

You need to do something similar for your fade out so it can be called each frame until alpha is 1 or whatever you determine.

I’m not sure what static function you mean, the only thing I could find is this static variable [http://docs.unity3d.com/ScriptReference/Input-mousePosition.html][1], but it’s read only. Thanks! [1]: http://docs.unity3d.com/ScriptReference/Input-mousePosition.html