x


switch between cameras in javascript?

how do you switch between cameras in javascript? I've tried

gameObject.Find("cam1").GetComponent("Camera").active = true/false

but it's not working, an error comes up that says "NullReferenceExeption"

so is there any other way?

more ▼

asked Aug 16 '10 at 08:27 PM

Unity Noob gravatar image

Unity Noob
36 6 6 7

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

i would use something like this :

var cam1 : Camera;
var cam2 : Camera;

function Update () {
     if(Input.GetKeyDown("1")){
          cam1.enabled = true;
          cam2.enabled = false;
     }
     if(Input.GetKeyDown("2")){
          cam1.enabled = false;
          cam2.enabled = true;
     }
}
more ▼

answered Aug 16 '10 at 09:11 PM

3dDude gravatar image

3dDude
2.6k 66 76 103

thank you! it worked

Aug 17 '10 at 08:47 AM Unity Noob

please mark a accepted answer if it worked!

Aug 17 '10 at 12:49 PM 3dDude

this doesn't work for me, for some reason I can't use the cameras as variabels, wel I'l have to look some more

Apr 23 at 01:12 PM yu-gi-master
(comments are locked)
10|3000 characters needed characters left

The Null reference exception is coming up because Unity cannot find a GameObject with name - "cam1". Instead you can try using an If condition to check if the GameObject exists and then change the settings.

I think the code should be something like this -

function Start() 
{
if(GameObject.Find("cam1"))
camera1 = GameObject.Find("cam1");
camera1.Camera.enabled = false/true;
} 
more ▼

answered Aug 16 '10 at 10:24 PM

diabloroxx gravatar image

diabloroxx
689 5 7 12

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x241
x51
x20

asked: Aug 16 '10 at 08:27 PM

Seen: 2812 times

Last Updated: Apr 23 at 01:12 PM