Switch Cameras and adding Gui

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?

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!

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;
}
}

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 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.

Why don’t instead using:

camera1.camera.enabled = false;

just write:

camera1.enabled = false;

?

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

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));
}

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

If so just make a little script CameraName1:

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:

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