Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by carnivoris · Jan 20, 2015 at 07:15 PM · nullreferenceexceptionmaincamera

Camera.main NullReferenceException on Instantiated Character

So, for some reason, my main camera (which is tagged as MainCamera) works fine if the game is run in the editor, but throws a nullreferenceexception when I build and run the game standalone. I can't find anything except "make sure the camera has the Main Camera tag" when looking for solutions. Are there any caveats to using MainCamera on a child of an instantiated object?

Comment
Add comment · Show 7
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 smoggach · Jan 20, 2015 at 07:26 PM 0
Share

I would assume there are. What is the $$anonymous$$ainCamera before you instantiate your object? If it's nothing then there's your problem. If it is something then the problem is probably where you switch it.

avatar image carnivoris · Jan 20, 2015 at 07:50 PM 0
Share

I have another camera that is enabled just to show the start menu/an overview of the level. I disable that camera and enable the main. It works fine in the editor, but it doesn't work in the standalone player.

 standbyCamera.SetActive(false);
 myPlayerGO.transform.FindChild("$$anonymous$$ain Camera").gameObject.SetActive(true);

I set the standbyCamera earlier in the script, but it works because I actually see the scene/menu and am able to interact. As soon as I click start, though, it seems like the $$anonymous$$ain Camera object doesn't get activated.

Am I trying to activate the main camera incorrectly? It seems correct, according to the Unity docs I've read.

avatar image smoggach · Jan 20, 2015 at 07:55 PM 0
Share

$$anonymous$$y first guess would be that FindChild is returning null. Switching main cameras is going to be a hassle no matter how you approach it. I would recommend having the camera prepared beforehand ins$$anonymous$$d of in the prefab.

avatar image carnivoris · Jan 20, 2015 at 07:58 PM 0
Share

It has to be in the prefab as I'm instantiating the character on the network.

EDIT: It does seem to work if I leave the main camera on the player character enabled. However, now my mouse look script doesn't want to work. That's a different issue, though. Thanks!

avatar image smoggach · Jan 20, 2015 at 08:04 PM 0
Share

Something has to instantiate the prefab. Whatever that is should be controlling the cameras. If you really really really want the camera inside a prefab then I'd recommend putting a script on your prefab that has a reference to the camera. Then you can get that script using GetComponent ins$$anonymous$$d of wasting time searching.

To switch the main camera: 1) Set the camera's tag to $$anonymous$$ainCamera in the prefab. 1) Load your prefab 2) Destroy or re-tag the current $$anonymous$$ainCamera.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

26 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

Related Questions

NullReferenceException in game build 1 Answer

Unity project 3.5 to 4 issues 0 Answers

Beginner having trouble with NullReferenceException 2 Answers

NullReferenceException when sending message to another object...really stumped. 1 Answer

Problem with Null Reference Exeption??:/ 1 Answer


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