- Home /
Fading To New Scene Problem?
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.
hate to state the obvious, but you're never calling EndScene()
from anywhere...
also u need this script attached to some game object in your level2. or else it will just fade to black and stay there.
Okay so I made some adjustments to two scripts. The one I posted, and my $$anonymous$$ain$$anonymous$$enuScript, which is the scene I want to fade into after my first scene. I tried calling the script from the $$anonymous$$ain$$anonymous$$enuScript, and here's how it looks:
public class $$anonymous$$ain$$anonymous$$enu : $$anonymous$$onoBehaviour {
GameObject scriptName;
void Start(){
scriptName.GetComponent<SceneFadeInOut> ().EndScene ();
}
In my $$anonymous$$ain $$anonymous$$enu Scene, I have the SceneFadeInOut attached to a GUI Texture, just like my first scene. It fades in when I play it, just like the first scene. I also get this error: NullReferenceException UnityEngine.GameObject.GetComponent[SceneFadeInOut] () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineGameObject.cs:28) $$anonymous$$ain$$anonymous$$enu.Start () (at Assets/Scripts/$$anonymous$$ainmenu/$$anonymous$$ain$$anonymous$$enu.cs:26)
So after all of this, the first scene is still not fading out and loading the $$anonymous$$ain $$anonymous$$enu Scene.
Answer by drex150 · Jul 21, 2014 at 06:26 AM
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);
}
}
}
Awesome thank you so much drex. Quick question, do you know how to stop it from continuously fading after it loads the Application.LoadLevel(2)? So like it fades in to the first scene, then fades out, then loads the next scene, and then it just keeps fading in over and over. So the screen on scene 2 becomes black over and over.
Never $$anonymous$$d I figured it out. Thanks a lot again drex.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to Set a bool to Individual Scenes (C#) 1 Answer
Third person movement similar to Max Payne 1 Answer
Scene changing 1 Answer
Make player not be seen by AI, when player in foilage and shadows. 1 Answer