- Home /
Switching Cameras
Hi there, I've been trying to switch between my cameras mid-game, but the same error keeps coming up. Here are the scripts I've been using:
var camera1 : Camera;
var camera2 : Camera;
//camera1 being the main camera, and camera2 being a still one that just overlooks
var IsSwitched = false;
var IsNotSwitched = true;
function Start () {
camera1.enabled = true;
camera2.enabled = false;
}
function Update () {
if(IsSwitched){
camera1.enabled = false;
camera2.enabled = true;
}
if (IsNotSwitched){
camera1.enabled = true;
camera2.enabled = false;
}
}
And here is the other script, attached to an object that when touched, switches the cameras:
var ScriptHolder : SwitchCamera1;
function OnTriggerEnter(hit : Collider) {
ScriptHolder.IsSwitched = true;
ScriptHolder.IsNotSwitched = false;
Destroy(gameObject);
}
And it works just fine with switching the cameras; however, this error came up, concerning the object that is the player (the [ ] is where the script is located, but isn't word-for-word the exact location):
NullReferenceException ThirdPersonController.UpdateSmoothedMovementDirection () (at [script location]:105) ThirdPersonController.Update () (at [script location]:320)
Note: I have the OrbitCamera script attached to the main camera, obviously focused upon the player object, and the ThirdPersonController script on the player.
The only problem is this error; everything else works fine. I'd like to know what I'm doing wrong here. Thanks for helping!
It would be ALRIGHT to submit new scripts to help answer this if it's preferable! I just need to switch the cameras, and they don't have to be with the exact scripts I provided here. Alterations are fine, too.
You're a genius!!! That WAS all I had to do! I just changed the tag of the stationary camera to $$anonymous$$ain Camera and the error didn't pop up. Thank you! You should convert your comment to an answer
Answer by AlucardJay · Nov 21, 2012 at 04:58 AM
It looks like the problem here is you have scripts on the character controller that are looking for Camera.main either you need to set the name and tag of both your cameras to Main Camera, or modify the script that throws the error to use the current camera instead of the main camera.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084203im_/https://answers.unity.com/themes/thub/images/avi.jpg)