Fade script working but I can't 'see' it working? (Solved)

Sorry for the cryptic title, but its actually quite accurate.

Searching the forums I’ve found this script that should fade-in and fade-out once my scene starts. I’m using unity5 which auto corrected the scripts and I’m getting no compiler errors.

If I play the game in window mode I can watch the inspector and things appear to be happening as they should - I can see the ‘faded’ variable changing from false - true. I can even see the alpha scale of the GUITexture decreasing and then increasing.

However on the screen (in-game) there is no visual representation of this. I mean nada… no blackness, no colour change… nothing.

I see all the appropriate Debug and printed messages, so I’m assuming the script is working correctly?

using UnityEngine;
using System.Collections;

public class CollideAndFade : 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.

	public bool Faded;
	
	private bool fadeNow;

		
	void Awake()
	{
		// Set the texture so that it is the the size of the screen and covers it.
		GetComponent<GUITexture>().pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);

		Faded = false;

		fadeNow = false;
	}
	

	void Update()
	{
		if(sceneStarting == true) // If the scene is starting...
		{
			StartScene(); // ... call the StartScene function.
		}

		if(fadeNow)
		{
			EndScene();
		}
	}
	
	
	void FadeToClear()
	{
		// Lerp the colour of the texture between itself and transparent.
		GetComponent<GUITexture>().color = Color.Lerp(GetComponent<GUITexture>().color, Color.clear, fadeSpeed * Time.deltaTime);
		print ("I'm running!");
	}
	    
		
	void FadeToBlack()
	{
		// Lerp the colour of the texture between itself and black.
		GetComponent<GUITexture>().color = Color.Lerp(GetComponent<GUITexture>().color, Color.black, fadeSpeed * Time.deltaTime);
	}
	
	
	void StartScene()
	{
		// Fade the texture to clear.
		FadeToClear();
		
		// If the texture is almost clear...
		if(GetComponent<GUITexture>().color.a <= 0.05f && sceneStarting)
		{
			// ... set the colour to clear and disable the GUITexture.
			GetComponent<GUITexture>().color = Color.clear;
			GetComponent<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.
			GetComponent<GUITexture>().enabled = true;

			// Start fading towards black.
			FadeToBlack();
			
			// If the screen is almost black...
			if(GetComponent<GUITexture>().color.a >= 0.95f)
			// ... reload the level.
			Application.LoadLevel(2);
		}
	}
}

I’m thinking its something to do with my GUITexture (maybe pixel inset) I’ve not used this before so its a good chance that’s where I’m being a noob.

42924-guitexture.png

I’ve tried a few variations for the X Y W H fields but nothing changed. The texture I’m using is a simple 32x32 black square.(Texture type : Texture)

What am I doing wrong please guys ???

I don’t see an immediate problem with the code (though it’s a bit messy and uses too many GetComponent calls without caching ^^).

I would suggest you simply use my AutoFade script which you just need to drop into your project and whenever you want to change to another scene you can use

AutoFade.LoadLevel(levelName ,fadeOutDuration, fadeInDuration, color);
// or
AutoFade.LoadLevel(levelIndex ,fadeOutDuration, fadeInDuration, color);

Example:

AutoFade.LoadLevel("level2", 1.5f, 1.5f, Color.black);

This will do the same as Application.LoadLevel, but before it loads the new scene it fades to the specified color and when the new scene is loaded it fades out again.

ps: You don’t need to attach the AutoFade script manually to anything. It will create an instance if needed.