How to "fade out" a scene

How I can make a scene go to another one, but by making the screen darker and darker…
I want it to be smooth

Drop a black texture in the inspector and place script on the camera.

#pragma strict

// FadeInOut

var fadeTexture : Texture2D;
var fadeSpeed = 0.2;
var drawDepth = -1000;

private var alpha = 1.0; 
private var fadeDir = -1;

function OnGUI(){
    
    alpha += fadeDir * fadeSpeed * Time.deltaTime;  
    alpha = Mathf.Clamp01(alpha);   
    
    GUI.color.a = alpha;
    
    GUI.depth = drawDepth;
    
    GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
}

That script will fade in … to fade out use this one

#pragma strict

// FadeInOut

var fadeTexture : Texture2D;
var fadeSpeed = 0.2;
var drawDepth = -1000;

private var alpha = 0.0; 
private var fadeDir = -1;

function OnGUI(){

    alpha -= fadeDir * fadeSpeed * Time.deltaTime;  
    alpha = Mathf.Clamp01(alpha);   

    GUI.color.a = alpha;

    GUI.depth = drawDepth;

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

I would like to post here the updated script that will work in unity-5.x

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class FadeScreen : MonoBehaviour {

    public Texture2D fadeTexture;

    [Range(0.1f,1f)]
    public float fadespeed;
    public int drawDepth = -1000;

    private float alpha = 1f;
    private float fadeDir = -1f;

	// Use this for initialization
	void Start () {
	
	}
	

    void OnGUI() {

        alpha += fadeDir * fadespeed * Time.deltaTime;
        alpha = Mathf.Clamp01(alpha);

        Color newColor = GUI.color; 
        newColor.a = alpha;

        GUI.color = newColor;

        GUI.depth = drawDepth;

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

    }

  

}

I managed to convert it into c# so if anyone is interested, here it is:

`
public bool fade;
public Texture2D fadeTexture;
public float fadeSpeed = 0.2f;
public int drawDepth = -1000;
private float alpha = 1.0f;
private int fadeDir = -1;

if (fade) {

		alpha += fadeDir * fadeSpeed * Time.deltaTime;  
		alpha = Mathf.Clamp01 (alpha);   
		// GUI.color = new Color() { a = 0.5f };
		GUI.color = new Color() {a=0.8f};
	
		GUI.depth = drawDepth;
	
		GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), fadeTexture);
			}

`

You could try lerping the alpha of a blank black GUITexture from invisible to visible, but I’m not sure how you would go about doing that.

public static IEnumerator FadeOutThreeD (this Transform t, float targetAlpha, bool isVanish, float duration)
{
Renderer sr = t.GetComponent ();
float diffAlpha = (targetAlpha - sr.material.color.a);

		float counter = 0;
		while (counter < duration) {
			float alphaAmount = sr.material.color.a + (Time.deltaTime * diffAlpha) / duration;
			sr.material.color = new Color (sr.material.color.r, sr.material.color.g, sr.material.color.b, alphaAmount);

			counter += Time.deltaTime;
			yield return null;
		}
		sr.material.color = new Color (sr.material.color.r, sr.material.color.g, sr.material.color.b, targetAlpha);
		if (isVanish) {
			sr.transform.gameObject.SetActive (false);
		}
	}



----------------------------------------------------------------------------------------------------------------------



    using UnityEngine;
    using System.Collections;
    
    public class VanishObject : MonoBehaviour
    {
    
    	void Start ()
    	{
    		
    		StartCoroutine (transform.FadeOutThreeD (0, true, 10));
    	}
    
    }