fading using ongui issue javascript

i’m havign an odd issue and it could be i just don’t know enough to onGui basically my issue is i have a scene before that fades out, then call another scene and begin a fade in.

once it’s faded in i want to fade it back out but it never fades out.

this is the code i have for the second scene i’m working with, the first scene works just fine.

public var theTexture : Texture2D;

private var StartTime : float;

public var fadeIn : boolean = false;



function OnGUI()

{

	if (fadeIn == false)

	{

		// set the color of the GUI

		GUI.color = Color.white;	

		// interpolate the alpha of the GUI from 1(fully visible)

		// to 0(invisible) over time

		GUI.color.a = Mathf.Lerp(0.0, 1.0, (Time.time/6));

		// draw the texture to fill the screen

		GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height), theTexture);

		if (GUI.color.a >= 1)

		{

			fadeIn = true;

			Debug.Log("full picture!");

		}

	}

	else

	{

		// set the color of the GUI

		GUI.color = Color.white;	

		// interpolate the alpha of the GUI from 1(fully visible)

		// to 0(invisible) over time

		GUI.color.a = Mathf.Lerp(1.0, 0.0, (Time.time/6));

		// draw the texture to fill the screen

		GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height), theTexture);

		if (GUI.color.a <= 0)

		{

			// we'll switch the scene.

			Debug.Log("Scene Switch!");

			Application.LoadLevel(2);

		}

	}

}

You should use coroutines, something like that (That’s not doing your thing right away, but you can work from there I guess)

private var alpha : float = 0;
var duration: float = 1;
function Start(){
    yield FadeIn();
    yield FadeOut();
    ...
}
function FadeIn(){
var d = 1f / duration;
    while( alpha < 1 ){ alpha += Time.deltaTime * d; yield; }
}
function FadeOut(){
var d = 1f / duration;
    while( alpha > 0 ){ alpha -= Time.deltaTime * d; yield; }
}
function NextLevel(){
    yield FadeOut();
    Appllication.LoadLevel(2);
}
function OnGUI(){
    GUI.color = Color(1,1,1, alpha );
}