Camera being disabled by any GUI button

Hello everyone,

I have a camera that should be enabled or disabled with a specific toggle button. The problem is that when it is on, if I press the mouse button on any other GUI button on the screen, the camera will be desabled. After I release the mouse button it will be turned back on. This happens with every GUI button/toggle on the screen.

Next is the code of C# class that handles the enabling/disabling of the camera. Don’t consider the Start and LateUpdate functions since they are used for other purposes:

using UnityEngine;
using System.Collections;

public class Configuration : MonoBehaviour {

    public Camera mainCamera;          //Para configurar su ViewPort 
    public Camera gizmoCamera;          //Para configurar su ViewPort
    public Camera mapCamera;          //Para configurar su ViewPort  
    public GUISkin skin;             //Para estilizar los componentes de la GUI para el BOX de herramientas

    private bool mapaButton;

    private int anchoFinal;              //Para alojar el ancho de la ventana final (según desktop) 
    private int altoFinal;           //Para alojar el alto de la ventana final (según desktop)   
    private bool viewPortConfiged = false; //para ejecutar sólo una vez el cambio de ViewPort

    void Awake(){
       //Inicialmente la cámara del mapa está encendida, por lo que se busca apagarla          

    void Start() {

       WindowsAPI wh = new WindowsAPI();

       //obtener los datos del desktop
       Rect workingArea = wh.GetWorkingArea();   //
       int ancho = (int) workingArea.width;
       int alto = (int) workingArea.height;
       int bordeVertical = wh.GetBorderHeight(); //
       int bordeHorizontal = wh.GetBorderWidth();    //
       int bordeCaption = wh.GetCaptionHeight(); //       

       anchoFinal = ancho + bordeHorizontal * 2;
       altoFinal = alto + bordeVertical*2;     
       if(Screen.width == 299)
         wh.MakePlayerWindow(ancho - bordeHorizontal * 2, alto - bordeCaption - bordeVertical*2, WindowState.Windowed);     

    void LateUpdate(){     
       //sólo configura si cambió la resolución y si no se ha ingresado antes
       if((Screen.width != 299) && (!viewPortConfiged))   {
         WindowsAPI wh = new WindowsAPI();
         //setea el ViewPort de cada cámara de acuerdo a la nueva resolución            
         mainCamera.pixelRect = new Rect(0,0, Screen.width, Screen.height - 31);                 
         mapCamera.pixelRect = new Rect(Screen.width - 159, Screen.height - 369 - 14, 159, 369);                 
         gizmoCamera.pixelRect = new Rect(0, 0, 100, 100);
         //Despliega la ventana utilizando la mayor parte del área del desktop
         viewPortConfiged = true;                  

    void OnGUI() { = skin;  

       GUI.Box (new Rect(0, 0, Screen.width, 31),"","GUI2-Box Herramientas"));        

       //Crea el texto "Mapa" y el botón que se encuentra inmediatamente al lado
       GUILayout.BeginArea (new Rect (Screen.width - 159, 0, 159, 30));  
          GUILayout.Label ("Mapa","GUI2-HeaderMapa"));  
          //Si se presiona el botón, se invocará ToggleMapCamera()                        
           mapaButton = GUILayout.Toggle(mapaButton,"Mapa");                       
       GUILayout.EndArea ();

       if(mapaButton) ToggleMapCamera();           


    void ToggleMapCamera(){         
       mapCamera.enabled = !mapCamera.enabled;


Thanks for the help.


Sebastián Toro

Here is another class (JS) that has another toggle button. Remember that this happens with every GUI button-type controller.

Just focus on the OnGUI function:

import System.Xml;

//XML con los layers
var listaCompletaLayers : TextAsset;
//Skin para la selección de layers
var guiSkin : GUISkin;

//Imágenes para el botón de encendido/apagado de los layers
var onTexture : Texture;
var offTexture : Texture;

private var layers : Array;
private var gameObjects : Array;
private var toolBoxVisible : boolean;
//almacena la posición del scrollbar
private var scrollPosition : Vector2 =;

function Start () {	
	//Cargar el XML
	var xmlDoc : XmlDocument = new XmlDocument();   	
   	//Obtener todos los nodos tipo Layer
   	var NodeList : XmlNodeList = xmlDoc.GetElementsByTagName("Layer");   	   	
   	//leer todos los nodos obtenidos y almacenarlos en el arreglo planos
   	layers = new Array();
	for (var layer : XmlNode in NodeList)
   		var objetoLayer : LayerClass = new LayerClass(layer.Attributes["tipo"].Value);     		
   	//encontrar los componentes y almacenar su referencia
	gameObjects = new Array();
	for (var layer : LayerClass in layers)
   		var componentes : GameObject[] = GameObject.FindGameObjectsWithTag(layer.GetTipo());

function OnGUI () { = guiSkin;
	//Crea el texto "Capas" y el botón que se encuentra inmediatamente al lado
	GUILayout.BeginArea (new Rect (0, 0, 159, 30));	
			GUILayout.Label ("Capas","GUI2-HeaderCapas"));		    					   			
			toolBoxVisible = GUILayout.Toggle(toolBoxVisible,"Capas");														
	GUILayout.EndArea ();									 
	if (toolBoxVisible) {		
		/* Con ToolBox en la esquina superior-izquierda */
		var ToolBoxScroll : boolean;
		var altoNecesario : int = layers.length * 18 + 5;
		var altoDisponible : int = Screen.height/2;	//383 corresponde a la barra más el Mapa
		var altoTollBox : int;
		var ptoInicioToolBox : int = 30; 			//En el 30 termina el ToolBox
		if (altoDisponible > altoNecesario){
			altoTollBox = altoNecesario;				
			ToolBoxScroll = false;
			altoTollBox = (Mathf.Round(altoDisponible/18) - 1)*18 + 5;			
			ToolBoxScroll = true;
	    GUILayout.BeginArea (Rect (0, 30, 200, altoTollBox),"GUI2-BoxCapas"));
				scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(197), GUILayout.Height(altoTollBox-5));
			        for (var layer : LayerClass in layers)
								GUILayout.Label (layer.GetTipo(),"LabelLayer_c_Scroll"));
								GUILayout.Label (layer.GetTipo(),"LabelLayer_s_Scroll"));
							var buttonSkin : Texture = (layer.estado) ? onTexture : offTexture;					
							if(GUILayout.Button (buttonSkin,"GUI2-ToggleCapas"))) ToggleLayer(layer.GetTipo());											
		GUILayout.EndArea ();				

function ToggleLayer (nombreLayer : String) {
	var indexLayer : int = 0;
	for (var layer : LayerClass in layers)
		if(layer.GetTipo() == nombreLayer){
			layer.estado = !layer.estado;	//hace un toggle al estado de la capa
			var arregloObjetosCapa : GameObject[] = gameObjects[indexLayer]	as GameObject[];
			for (var componente : GameObject in arregloObjetosCapa)  {      = !;		//Hace un Toggle al estado del objeto
			//apagar todos los componentes de dicho layer

It seems to me that if the button is ever checked, then ToggleMapCamera is going to be called every frame since GUILayout.Toggle will always return true. In fact, it could be called multiple times per frame according to the OnGUI documentation.

The reason you’re seeing the odd behavior is likely that in the normal case ToggleMapCamera is being called twice per frame, so it’s toggling the camera on, then off. But when you click a different button it’s being called an odd number of times.

Rather than toggling the camera, you should just turn it on or off based on the state of the toggle…

mapCamera.enabled = mapaButton;