Turn off camera dark screen on Unity 5 (c#)

This is a method I used to turn off camera in Blind

Unity turn off camera

Setting Camera.main.cullingMask to Zero

using UnityEngine;
using System.Collections;

/********************************************
 UNITY 5.2.3 Blind Project '15          
 Turn ON / Turn OFF Camera & Lights        
 v 1.0                         
 www.Finalmarco.com                
/********************************************/



public class SpegniLuci : MonoBehaviour {
	// Use this for initialization
	void Start () {

        //Turn Off on start
        Camera.main.clearFlags = CameraClearFlags.SolidColor;
		Camera.main.backgroundColor = Color.black;
		Camera.main.cullingMask = 0;
    }

	void spegni(){ // Turn Off
		Camera.main.clearFlags = CameraClearFlags.SolidColor;
		Camera.main.backgroundColor = Color.black;
		Camera.main.cullingMask = 1;
    }

	void accendi(){ // Turn On
		Camera.main.clearFlags = CameraClearFlags.SolidColor;
		Camera.main.backgroundColor = Color.black;
		Camera.main.cullingMask = 0;

    }

	// Update is called once per frame
	void Update () {

        //	HOLD KEY T than press Y, CTRL do not work as expected
        if ((Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.T)) && Input.GetKeyDown(KeyCode.Y))
        {
            if (Camera.main.cullingMask == 1)
            {
                accendi();
            }
            else
            {
                spegni();
            }

        }
    }
}

More info : http://docs.unity3d.com/ScriptReference/Camera-cullingMask.html

Questions? Suggestions? Please leave a comment below.

Leave a comment