Hello, I’m trying to fade one scene to another, but I’m having trouble. The screen fades in into the first scene I have loaded, but it does not fade out into another scene. I don’t know why this is happening, so maybe I’m not seeing something. Here’s the script so you guys can see for yourselves:
public float fadeSpeed = 1.5f; // Speed that the screen fades to and from black.
private bool sceneStarting = true; // Whether or not the scene is still fading in.
private bool Faded;
void Awake ()
{
// Set the texture so that it is the the size of the screen and covers it.
guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
Faded = false;
}
void Update ()
{
// If the scene is starting...
if(sceneStarting)
// ... call the StartScene function.
StartScene();
}
void FadeToClear ()
{
// Lerp the colour of the texture between itself and transparent.
guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
}
void FadeToBlack ()
{
// Lerp the colour of the texture between itself and black.
guiTexture.color = Color.Lerp(guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);
}
void StartScene ()
{
// Fade the texture to clear.
FadeToClear();
// If the texture is almost clear...
if(guiTexture.color.a <= 0.05f)
{
// ... set the colour to clear and disable the GUITexture.
guiTexture.color = Color.clear;
guiTexture.enabled = false;
Faded = true;
Debug.Log("True");
// The scene is no longer starting.
sceneStarting = false;
}
}
void EndScene ()
{
if (Faded == true)
{
// Make sure the texture is enabled.
guiTexture.enabled = true;
// Start fading towards black.
FadeToBlack();
// If the screen is almost black...
if(guiTexture.color.a >= 0.95f)
// ... reload the level.
Application.LoadLevel(2);
}
}
}
So why is it not fading to a different scene? The part that’s not working is the function EndScene. It’s not setting the guiTexture to true, and is not loading a new scene. By the way this script was from a tutorial. I simply added and changed some things like the LoadLevel, the if statement, the Faded bool, and so on.
Your StartScene(); is inside the function Update. function Update runs every frame so StartScene(); is running every frame until sceneStarting becomes false.
On the other hand, EndScene(); is never being called, let alone not being called in the update function. So even if you are calling it elsewhere, if you’re not calling it in the update, it is only going to run one time and then stop until you tell it to run again.
Basically, you need EndScene(); to be called in the update function as long as some kind of variable is true.
Here is a very simple version of your code that fades to clear and then automatically turns back around and fades to black.
Notice EndScene(); is in the update with a fadeNow variable. Just make fadeNow true whenever you want the screen to fade out.
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
public float fadeSpeed = 1.5f; // Speed that the screen fades to and from black.
private bool sceneStarting = true; // Whether or not the scene is still fading in.
private bool Faded;
private bool fadeNow;
void Awake ()
{
// Set the texture so that it is the the size of the screen and covers it.
guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
Faded = false;
fadeNow = false;
}
void Update ()
{
// If the scene is starting...
if(sceneStarting)
// ... call the StartScene function.
StartScene();
if(fadeNow)
{
EndScene();
}
}
void FadeToClear ()
{
// Lerp the colour of the texture between itself and transparent.
guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
print ("I'm running!");
}
void FadeToBlack ()
{
// Lerp the colour of the texture between itself and black.
guiTexture.color = Color.Lerp(guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);
//print ("I'm running!");
}
void StartScene ()
{
// Fade the texture to clear.
FadeToClear();
// If the texture is almost clear...
if(guiTexture.color.a <= 0.05f && sceneStarting)
{
// ... set the colour to clear and disable the GUITexture.
guiTexture.color = Color.clear;
guiTexture.enabled = false;
Faded = true;
Debug.Log("True");
// The scene is no longer starting.
sceneStarting = false;
}
if(!sceneStarting)
{
fadeNow = true;
}
}
void EndScene ()
{
if (Faded == true)
{
// Make sure the texture is enabled.
guiTexture.enabled = true;
// Start fading towards black.
FadeToBlack();
// If the screen is almost black...
if(guiTexture.color.a >= 0.95f)
// ... reload the level.
Application.LoadLevel(2);
}
}
}