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 gilsdank · Jan 27, 2014 at 06:06 AM · raycastrayscreenpointtoray

Why won't my Ray initialization work??

I am using a Raycast from my mouse position to select an object in 3d space but my code is coming up with error NullReferenceException: Object reference not set to an instance of an object CameraControl.CameraSelect () (at Assets/Scripts/CameraControl.cs:32)

here is my code

     void CameraSelect() {        
         RaycastHit rayHit;
         if(Input.GetButton("Fire1")){
             Debug.Log(curCam);
             Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out rayHit,9999)){
                 curCam.enabled = false;            
                 tarCam = rayHit.transform.gameObject.GetComponentInChildren< Camera >();
                 tarCam.enabled = true;
             }
         }
     }

it seems that my ray initialization is giving me the error, that is line 32, but from what i can tell my code is correct? please help

the error is on line 5 of the code snippet

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
1

Answer by GameVortex · Jan 27, 2014 at 09:00 AM

Your Ray initialization itself is correct.

The problem comes from the use of Camera.current which only has a value in the OnRenderImage, OnPreRender and OnPostRender events, because it references the currently rendering camera. Outside of those events you would want to use Camera.main instead (referencing the camera tagged "MainCamera"), or reference the camera you want to use yourself through a public camera variable.

Comment
Add comment · Show 3 · 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 gilsdank · Jan 27, 2014 at 08:39 PM 0
Share

Well I need to reference the current camera being displayed, I have a public variable that holds the current camera named curCam, could I use Camera.curCam.ScreenPointToRay?

avatar image GameVortex · Jan 28, 2014 at 07:54 AM 0
Share

If you have a public variable curCam in the same class as the CameraSelect function you can use it directly: curCam.ScreenPointToRay.

avatar image gilsdank · Jan 28, 2014 at 07:56 AM 0
Share

Alright I'll try that and I'll post when I get home. Thanks for your help!

avatar image
0

Answer by Shrikky23 · Jan 27, 2014 at 06:24 AM

Hi this is how you create a ray public Ray ray = new Ray(transform.position, transform.forward);

So in your case public Ray ray = new Ray(Input.mousePosition , target position);

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 gilsdank · Jan 27, 2014 at 07:40 AM 0
Share

i dont know the target position though, im just trying to see if there is an object where i clicked and if so then to switch the current camera to the objects child camera

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

20 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

Related Questions

Follow mouse cursor (with Y = 0)? 1 Answer

Why does ScreenPointToRay bug when called from IEnumerators? 1 Answer

Why is the camera.screenpointtoray off? 1 Answer

Not getting right value; ScreenPointToRay 0 Answers

camera.ScreenPointToRay always has same origin... 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