guiTexture Fadein / out issue

Hey guys,

Got a weird issue and I hope someone can help. I’m using the tutorial ScreenFader to fade in and out a gui texture. However, I am calling it from another call and somehow, this causes the FadeToBlack() function to only partially fade back in the alpha?

Here is the code that calls the function when I hit a trigger. The screenFader being the GUI.Texture with the Fader script on it…

void Start()
{
	roomMaster = GameObject.FindWithTag("ROOMMASTER");
	screenFader = GameObject.FindWithTag("Fader");
	player = GameObject.FindWithTag("Player");
	fadeCam = screenFader.GetComponent<ScreenFader>();
	levelToLoad = roomMaster.GetComponent<RoomMaster>();
}

void OnTriggerEnter (Collider other)
{
	fadeCam.EndScene();
	myLevelID = levelToLoad.RandomRoom(1);
	Debug.Log(myLevelID);
	StartCoroutine (Teleport ());
}

fadeCam.EndScene() Get called, however it only does it for a split second, and never complets at 100%. Here is that code. Note that me FadeToClear works perfectly?

public void FadeToClear(){

	guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
}

public void FadeToBlack(){

	guiTexture.color = Color.Lerp(guiTexture.color, Color.gray, fadeSpeed * Time.deltaTime);
}

public void StartScene(){

	FadeToClear();
	if(guiTexture.color.a <= 0.05f)
	{
		guiTexture.color = Color.clear;
		guiTexture.enabled = false;
		sceneStart = false;
	}
}

public void EndScene()
{
	guiTexture.enabled = true;
	FadeToBlack();
}

Any idea what I am doing wrong?

Thanks guys,
-EpicCel

You call fadetoack once so it lerps once and doesn’t continue. You need to put that in a reoccurring block. Like an update loop or a coroutine.

For example:

IEnumarator FadeToBlack()
    {
        float time = 0;
        while(time < 1)
        {
            time += Time.deltaTime;
            guiTexture.color = Color.Lerp(guiTexture.color, Color.black, time);
            yield return new WaitForEndOfFrame()
        }
    
        guiTexture.color = Color.black;
    
        
    }