- Home /
Yet another strange NullReferenceException
I'm trying to switch from my main camera, to a secondary camera, and back again to the main camera.
The script is quite simple. When I switch to my secondary, I disable the main camera like so:
//Disable the main camera
Camera.main.enabled = false;
My script works perfectly, switching to and enabling the secondary camera.
And when I want to re-enable the main camera:
//Re-enable the main camera
Camera.main.enabled = true;
But this throws a NullReferenceException:
NullReferenceException: Object reference not set to an instance of an object
FirstPersonViewer.focusListen () (at Assets/Scripts/FirstPersonViewer.cs:68)
FirstPersonViewer.Update () (at Assets/Scripts/FirstPersonViewer.cs:31)
Yes, my camera is still tagged as the main camera:
I have also tried climbing up the hierarchy and enabling it another way like this:
ObjectStaticProperties.RTS_CameraScript.gameObject.GetComponent<Camera>().enabled = true;
But it throws the same error. I seem to get strange NullReferenceExceptions constantly. I'm not sure if anyone knows what the cause of this might be, but I'd appreciate any help!
Cheers, Vranvs
Camera.main returns "the first enabled camera tagged "$$anonymous$$ainCamera" (Read Only)".
That's why you're getting the null reference when it's disabled.
Answer by Fredex8 · Mar 17, 2016 at 04:25 AM
Is the secondary camera also tagged as MainCamera? If not something in your scene is probably looking for a MainCamera and gives a null reference when you disable it.
This is the script I use, I can't remember where I found it:
void SwitchCameras()
{
cameraChange = !cameraChange;
if (cameraChange == true)
{
firstCamera.SetActive(true);
secondCamera.SetActive(false);
}
else
{
firstCamera.SetActive(true);
secondCamera.SetActive(false);
}
}
Guess that could probably be optimised a bit but it ensures a main camera is always in the scene but only one is active, provided both of your cameras are tagged MainCamera and the secondary is set to inactive at start or in the editor. Seems like a good method to me as it cuts down on active cameras in the scene but I don't know if you are still using your secondary camera to render something else when main is active?
I had to do some weird hacky stuff to fix my issue, but I will try this in the meantime.
The secondary camera is not tagged as main camera, and parts of the script attached to the camera were running while the script was disabled, while other parts were not. VERY weird stuff. I think it might be a bug, so I'll continue on and see if I have any more issues. But thanks for the reply.
Using enabled = false should just disable the camera and not touch the script so perhaps some parts of that script or another accessing it were relying on the camera being enabled. SetActive is a bit safer in that regard since any scripts are also disabled with the camera but it depends if you need the camera script to retain variables and such from before the switch.
Answer by Bunny83 · Mar 17, 2016 at 05:29 AM
The answer is pretty simple you just have to read carefully:
The first enabled camera tagged "MainCamera"
Returns null if there is no such camera in the scene.
Since you disabled your main camera there is no enabled camera anymore that is tagged as main camera.
If you want to switch between cameras you should tag them both with MainCamera and save references in your script to both cameras like @Fredex8 does in his example. Just declare two public Camera variables and assign both cameras to those variables in the inspector.
Your answer
![](https://koobas.hobune.stream/wayback/20220612072138im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Null reference exception on Screenpointtoray (Multiplayer) 1 Answer
NullRefenceException error 1 Answer
Converting to 3rd Person from 1st Person - Island Demo 1 Answer
NullReferenceException 2 Answers