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
3
Question by chillypacman · Oct 05, 2011 at 09:40 AM · c#camera

When is current camera null?

I'm writing a UI system where I draw mesh's orthographically in worldspace and position them using the camera class.

Basically I get extreme points of the screen like so:

 public static float CamRight
 {
    return RealCamera.ScreenToWorldPoint(new Vector3(Camera.current.pixelWidth,0,0)).x;
 }

What I'm finding though is occasionally Camera.current is turning up null. I find this weird because it's null or not from the same place it's being called, so that means sometimes when it's called it's returning null and sometimes not but it's being called by the same instance of the same game object...

So I was just wondering if there were some rules I could follow for this?

Comment
Add comment
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

2 Replies

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

Answer by CHPedersen · Oct 05, 2011 at 11:46 AM

Camera.current refers to the Camera that Unity is currently rendering things with. I can't tell you exactly why it turns up null, except this feeble guess:

Are you test-running your application in the editor with the sceneview displayed as well? Camera.current is not just your own application's camera, it is whatever is currently rendering, so sometimes, it actually refers to the editor's scene view camera. This camera will return null if you reference it without the scene view having focus, which is explained in this Question, also.

According to the docs, you should probably be using Camera.main, but that property returns whichever camera is tagged as the Main Camera. So if you need to refer to the camera that is currently actively rendering in your application, you need to keep track of them yourself.

I have the same functionality going in my application - to get around it I've simply declared a public static Camera variable in a static class. I set this variable whenever I change cameras, so it always refers to the camera I'm changing to.

Comment
Add comment · Show 1 · 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 _Shockwave · Mar 03, 2013 at 03:19 AM 0
Share

Thank you. I had this problem too.

avatar image
0

Answer by Smurjo · Jun 12, 2021 at 09:36 AM

I had the same problem and observed: With only one camera in the scene Camera.current is null whereas Camera.main contains the camera (it is tagged main camera).

I think this is a suboptimal implementation from Unity, as I will need to change the code should I use more cameras in future.

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

the code of camera rig in tanks Game can explain how this works? 0 Answers

Problem with Camera rotation script 3 Answers

Smoothing random noises with different amplitudes 0 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