Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by nickostan · Mar 17, 2016 at 03:09 AM · cameranullreferenceexception

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:

alt text

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

help1.png (6.6 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flashframe · Mar 17, 2016 at 04:41 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nickostan · Mar 17, 2016 at 04:42 AM 0
Share

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.

avatar image Fredex8 nickostan · Mar 17, 2016 at 05:06 AM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

52 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Null reference exception on Screenpointtoray (Multiplayer) 1 Answer

NullRefenceException error 1 Answer

Im Getting an error saying: Object reference not set to an instance of an object although nothing is null. 1 Answer

Converting to 3rd Person from 1st Person - Island Demo 1 Answer

NullReferenceException 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges