# Switch Cameras and adding Gui

**URL:** https://discussions.unity.com/t/switch-cameras-and-adding-gui/391419
**Category:** UI
**Tags:** IMGUI
**Created:** [May 5, 2009, 8:14am UTC](https://discussions.unity.com/t/switch-cameras-and-adding-gui/391419 "2009-05-05T08:14:42Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_78612932](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@anon\_78612932](https://discussions.unity.com/u/anon_78612932)
#### Post date: [May 5, 2009, 8:14am UTC](https://discussions.unity.com/t/switch-cameras-and-adding-gui/391419/1 "2009-05-05T08:14:42Z")

</div>

in my scene i have 4 cameras,  
is there a way for me to attach a gui.label for each camera but disappear when switching cameras?

---

<div class="post-metadata">

### Author: ![anon\_85768832](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@anon\_85768832](https://discussions.unity.com/u/anon_85768832)
#### Post date: [May 5, 2009, 7:27pm UTC](https://discussions.unity.com/t/switch-cameras-and-adding-gui/391419/2 "2009-05-05T19:27:20Z")

</div>

In your OnGUI function just check which camera you are currently using and display the appropriate label for that camera. Alternatively, you can have separate scripts attached to each camera that show the camera-specific GUI displays you want, then when you toggle cameras simply set a flag to enable/disable each script as necessary.

Many ways to do it, all should be relatively straightforward so let us know if the above makes sense or not as well as if more help is needed!

---

<div class="post-metadata">

### Author: ![anon\_78612932](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@anon\_78612932](https://discussions.unity.com/u/anon_78612932)
#### Post date: [May 6, 2009, 5:47am UTC](https://discussions.unity.com/t/switch-cameras-and-adding-gui/391419/3 "2009-05-06T05:47:16Z")

</div>

everytime i attach a on gui function to each of the seprate cameras,  
its stays on screen, even after i switch between the 4 cameras,

What script would i use to add the gui to each camera and then for it to onli stay in the assigned cameras view?

Camera Switch script:-

var camera0 : GameObject;  
var camera1 : GameObject;  
var camera2 : GameObject;

function Start () {  
camera0.camera.enabled = true;  
camera1.camera.enabled = false;  
camera2.camera.enabled = false;  
}

function Update () {  
if (Input.GetKeyDown (“1”)){  
camera0.camera.enabled = false;  
camera1.camera.enabled = true;  
camera2.camera.enabled = false;

}  
if (Input.GetKeyDown (“2”)){  
camera0.camera.enabled = false;  
camera1.camera.enabled = false;  
camera2.camera.enabled = true;  
}  
}

---

<div class="post-metadata">

### Author: ![anon\_85768832](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@anon\_85768832](https://discussions.unity.com/u/anon_85768832)
#### Post date: [May 6, 2009, 6:31pm UTC](https://discussions.unity.com/t/switch-cameras-and-adding-gui/391419/4 "2009-05-06T18:31:49Z")

</div>

The “problem” here is that you’re only telling the cameras themselves to enable/disable (to start/stop rendering what they “see”). You need to add a second tier of code to actually enable/disable any scripts that you have attached to those cameras so their respective GUI displays can show/hide. This can be done in a few ways, notably:

1. Use GetComponent to get a reference to the script component attached to each camera and then toggle the [enabled](http://unity3d.com/support/documentation/ScriptReference/Behaviour-enabled.html) property to turn the entire script on/off as needed.

2. Use GetComponent to get a reference to the script component attached to each camera and then toggle some custom property (like ‘IsMyGUIEnabled’) that you use inside your OnGUI functions to determine if you show that script’s UI or not.

---

<div class="post-metadata">

### Author: ![Lisan](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lisan/32/599895_2.png) [@Lisan](https://discussions.unity.com/u/Lisan)
#### Post date: [June 24, 2009, 6:23pm UTC](https://discussions.unity.com/t/switch-cameras-and-adding-gui/391419/5 "2009-06-24T18:23:35Z")

</div>

Why don’t instead using:

```auto
camera1.camera.enabled = false;

```

just write:

```auto
camera1.enabled = false;

```

?

Didn’t it disable whole gameobject with camera component and scripts in one call?

---

<div class="post-metadata">

### Author: ![fis](https://avatars.discourse-cdn.com/v4/letter/f/c77e96/32.png) [@fis](https://discussions.unity.com/u/fis)
#### Post date: [September 17, 2009, 12:17pm UTC](https://discussions.unity.com/t/switch-cameras-and-adding-gui/391419/6 "2009-09-17T12:17:23Z")

</div>

One more variant, with menu.

var windowRect : Rect = Rect (150, 20, 120, 80);  
var customSkin : GUISkin;  
var camera1 : GameObject; // default fps camera  
var camera2 : GameObject;  
var camera3 : GameObject;  
var camera4 : GameObject;  
var camera5 : GameObject;  
var fpscntrl : GameObject; // this is for first person controller

function OnGUI () {  
GUI.skin = customSkin;  
windowRect = GUI.Window (0, windowRect, DoMyWindow, “Select Camera View”);

}

function DoMyWindow (windowID : int) {  
// Draw any Controls inside the window here

// Make the first button.  
if (GUI.Button (Rect (5,25,126,20), “1stPerson”)) {  
camera1.gameObject.active = true;  
camera2.gameObject.active = false;  
camera3.gameObject.active = false;  
camera4.gameObject.active = true;  
camera5.gameObject.active = false;  
fpscntrl.gameObject.active = true;  
}  
// Make the second button.  
if (GUI.Button (Rect (5,55,126,20), “TopView”)) {  
camera1.gameObject.active = false;  
camera2.gameObject.active = true;  
camera3.gameObject.active = false;  
camera4.gameObject.active = false;  
camera5.gameObject.active = false;  
fpscntrl.gameObject.active = false;  
}  
if (GUI.Button (Rect (5,85,126,20), “Orbit”)) {  
camera1.gameObject.active = false;  
camera2.gameObject.active = false;  
camera3.gameObject.active = true;  
camera4.gameObject.active = false;  
camera5.gameObject.active = false;  
fpscntrl.gameObject.active = false;  
}  
if (GUI.Button (Rect (5,115,126,20), “Free1stPerson”)) {  
camera1.gameObject.active = false;  
camera2.gameObject.active = false;  
camera3.gameObject.active = false;  
camera4.gameObject.active = false;  
camera5.gameObject.active = true;  
fpscntrl.gameObject.active = false;  
}  
// GUI.DragWindow (Rect (0,0,10000,10000));  
}

---

<div class="post-metadata">

### Author: ![anon\_627234](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@anon\_627234](https://discussions.unity.com/u/anon_627234)
#### Post date: [September 17, 2009, 4:41pm UTC](https://discussions.unity.com/t/switch-cameras-and-adding-gui/391419/7 "2009-09-17T16:41:22Z")

</div>

Are you just wanting to be able to tell which camera you’re currently looking through?

If so just make a little script CameraName1:

```auto
def OnGUI():
    GUI.Label(Rect(0, 0, 200, 20), "Camera One")

```

And make four of those with different labels and attach them to the appropriate camera object.

---

<div class="post-metadata">

### Author: ![Bampf](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@Bampf](https://discussions.unity.com/u/Bampf)
#### Post date: [September 17, 2009, 6:30pm UTC](https://discussions.unity.com/t/switch-cameras-and-adding-gui/391419/8 "2009-09-17T18:30:32Z")

</div>

> [@""](#):
>
> Are you just wanting to be able to tell which camera you’re currently looking through?
> 
> If so just make a little script CameraName1:
> 
> ```auto
> def OnGUI():
> GUI.Label(Rect(0, 0, 200, 20), "Camera One")
> 
> ```
> 
> And make four of those with different labels and attach them to the appropriate camera object.

I like it; short and sweet. But won’t this have the same problem of always displaying the name of each camera? I think it just needs a check if the object is active.

Also, instead of hardcoding “Camera One” the script could just grab the name of the camera itself. Then the exact same script could be used on each camera. Something like:

```auto
function OnGUI () {
	if ( gameObject.active )
	{
		GUI.Label(Rect(0, 0, 200, 20), gameObject.name);
	}
}

```
