- Home /
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?
Answer by 3dDude · Aug 16, 2010 at 09:11 PM
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; } }
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
how for same question but it using one key for switching?ty:)
Answer by diabloroxx · Aug 16, 2010 at 10:24 PM
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;
}