Fade In each level

Hi there

I am trying out fading in when I start each level of my game, but my problem is that it only happens in the the beginning of my game and I want to fade in each time I go to a different scene of my game. I can’t figure out why it only load once in the beginning and ever again.

Here is my code:

var fadeTexture : Texture;
var startColor : Color = Color(0,0,0,1);
var endColor : Color = Color(0,0,0,0);
var duration : float = 2.0;
internal var currentColor : Color;

function Start () {

	currentColor = startColor;
	Destroy(gameObject, duration + 1);
}

function OnGUI() {

	GUI.depth = -10;
	GUI.color = currentColor;
	GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
}

function FixedUpdate () {

	currentColor = Color.Lerp(startColor, endColor, Time.time/duration);
}

Time.time is the time from the beginning of your game. It doesn’t restart when you load another scene. So use

private var startTime : float;

function Start()
{
    // [...]
    startTime = Time.time;
}

function FixedUpdate ()
{
    currentColor = Color.Lerp(startColor, endColor, (Time.time-startTime)/duration);
}