Scene GUI.color not effecting texture?

I’ve been working through some of the Unity tutorials, and I’ve come across the screen fader example here:

I was trying to use this bit of code in my own project, however I have some GUI buttons that are rendered on top of the GUITexture. Therefore, I tried to make some modifications to this script so I could fade all the GUI elements as well as the things within the scene.

using UnityEngine;
using System.Collections;

public class SceneFadeInOut : MonoBehaviour {

	public float fadeSpeed = 1.5f;
	public Texture fadeTexture;
	public string nextScene = "NoScene";

	private bool sceneStarting = true;
	
	void OnGUI ()
	{
		GUI.enabled = true;
		GUI.color = Color.black;
		GUI.DrawTexture (new Rect(0f, 0f, Screen.width, Screen.height/2), fadeTexture,
		                 ScaleMode.StretchToFill, true);
	}
	
	void Update()
	{
		if (sceneStarting)
		{
			StartScene ();
		}
	}
	
	void FadeToClear()
	{
		GUI.color = Color.Lerp(GUI.color, Color.clear, fadeSpeed * Time.deltaTime);
	}
	
	void FadeToBlack()
	{
		GUI.color = Color.Lerp (GUI.color, Color.black, fadeSpeed * Time.deltaTime);
	}
	
	void StartScene()
	{
		FadeToClear();
		if (GUI.color.a <= 0.05f)
		{
			GUI.color = Color.clear;
			GUI.enabled = false;
			sceneStarting = false;
		}
	}
	
	public void EndScene()
	{
		GUI.enabled = true;
		FadeToBlack();
		if (GUI.color.a >= 0.95f && nextScene != "NoScene")
		{
			Application.LoadLevel("MainMenu");
		}
	}
}

However, this just renders a black screen. I’ve tried to redraw the texture again by putting GUI.DrawTexture within the FadeToClear and FadeToBlack functions, however that throws null pointer exceptions. Not sure what to do here.

OnGUI is called every frame. You set the color inside OnGUI to black, so it will be black every frame. You also can’t change anything GUI related outside of OnGUI. Also GUI.enabled doesn’t enable or disable the gui itself but just disable interaction with the gui controls. You can’t disbale the gui that way. If you want to prevent certain things from showing, you can wrap those things in an if statement inside OnGUI or disable the whole script.

To get the fading working, you have to use your own local color variable and assign that one in OnGUI.

using UnityEngine;
using System.Collections;
 
public class SceneFadeInOut : MonoBehaviour
{
    public float fadeSpeed = 1.5f;
    public Texture fadeTexture;
    public string nextScene = "NoScene";
    
    private Color guiColor;
    private bool sceneStarting = true;
 
    void OnGUI ()
    {
        GUI.color = guiColor;
        GUI.DrawTexture (new Rect(0f, 0f, Screen.width, Screen.height/2), fadeTexture, ScaleMode.StretchToFill, true);
    }
    
    void Update()
    {
        if (sceneStarting)
        {
            StartScene ();
        }
    }
 
    void FadeToClear()
    {
        guiColor = Color.Lerp(guiColor, Color.clear, fadeSpeed * Time.deltaTime);
    }
    
    void FadeToBlack()
    {
        guiColor = Color.Lerp (guiColor, Color.black, fadeSpeed * Time.deltaTime);
    }
    
    void StartScene()
    {
        FadeToClear();
        if (guiColor.a <= 0.05f)
        {
            guiColor = Color.clear;
            sceneStarting = false;
        }
    }
 
    public void EndScene()
    {
        FadeToBlack();
        if (guiColor.a >= 0.95f && nextScene != "NoScene")
        {
            Application.LoadLevel("MainMenu");
        }
    }
}

ps: I’m not sure how exactly this script should work and from where you call EndScene, but i’ve written a fade in / out script some time ago if you’re interested: