Ok what I’m having trouble with is trying to fade out my scene when it ends. I have a gameobject called screenFader with this code (it’s from the unity stealth tutorial under screen fader.)
#pragma strict
public var fadeSpeed : float = 1.5f; // Speed that the screen fades to and from black.
private var sceneStarting : boolean = true; // Whether or not the scene is still fading in.
function 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);
}
function Update ()
{
// If the scene is starting...
if(sceneStarting)
// ... call the StartScene function.
StartScene();
}
function FadeToClear ()
{
// Lerp the colour of the texture between itself and transparent.
guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
}
function FadeToBlack ()
{
// Lerp the colour of the texture between itself and black.
guiTexture.color = Color.Lerp(guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);
}
function 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;
// The scene is no longer starting.
sceneStarting = false;
}
}
public function EndScene ()
{
// 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(0);
}
I also have a Guitext with this code:
#pragma strict
var fadeScreen : GameObject;
function Start(){
guiText.material.color.a = 0;
yield WaitForSeconds(2);
FadeIn();
}
function FadeIn(){
while (guiText.material.color.a < 1){
guiText.material.color.a += 0.1 * Time.deltaTime * 2;
yield;
}
yield WaitForSeconds(1);
FadeOut();
}
function FadeOut(){
while (guiText.material.color.a > 0){
guiText.material.color.a -= 0.1 * Time.deltaTime * 2;
yield;
}
Destroy (gameObject);
fadeScreen.GetComponent(sceneFadeInOut).EndScene();
}
So basically, what’s going on here is my scene fades in from black. Then some text fades in then fades out. Then the function EndScene() is called from the other script and the scene should fade out when the text has finished fading out and gets destroyed, but it doesn’t. Everything works as it should except for the scene fading out, it doesn’t happen. I’m not getting any errors or warnings. It’s almost as if the function never actually gets called. Am I missing something?
I don’t see you calling EndScene() anywhwere. Seems this could be your problem.