Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Burrowmonster · Jun 17, 2016 at 01:49 AM · cameraunity 2dtouchbug-perhapsscreentoworldpoint

UnityEngineCameraBindings.gen.cs throwing a NullReferenceException when using the Camera.ScreenToWorldPoint() Method.

A portion of my code is supposed to convert each touches screen pixel coordinates to a Vector2 World Point (Unity transform units). When I run the script (and touch the touchscreen I have connected), I get a NullReferenceException, yet VisualStudio never reported any problems within the script that I wrote. When I checked the Error List I got this feedback: alt text !Here's a paste able version of the code in the picture:

 void Update()
         {
             for (int i = 0; i < Input.touches.Length; i++)
             {
                 Camera camera = new Camera();
                 Vector3 worldPoint3d = camera.ScreenToWorldPoint(new Vector3(Input.touches[i].position.x,Input.touches[i].position.y,0));
                 Vector2 worldPoint2d = new Vector2(worldPoint3d.x, worldPoint3d.y);
     }
     }

The first thing I noticed was that the error was reported on line 458, while the script i'm writing currently only has 55 lines. I also noticed that it was reported in a file called "UnityEngineCameraBindings.gen.cs". The code you see in the picture does in fact use the UnityEngine.Camera.ScreenToWorldPoint() which is a Unity documented method. I may be an amateur programmer but I can deduce that the problem may not lie with my script, rather somewhere in the Camera class of the UnityEngine namespace.

I'd appreciate any suggestions on how I might be able to fix this, feedback on whether it's just me that's experiencing it or if its bug in the latest Unity update (5.3.5f1), or possibly any alternative way of scripting the function that the code in the picture performs.

problem.png (53.2 kB)
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

1 Reply

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

Answer by btmedia14 · Jun 17, 2016 at 04:00 AM

Try changing

 Camera camera = new Camera();

to

 Camera camera = GetComponent<Camera>();

This will obtain the camera game object from the scene, as opposed to creating a new camera which is likely unattached to the scene.

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 Burrowmonster · Jun 17, 2016 at 04:35 AM 0
Share

Thanks for pointing that out. I realized that Unity does not just automatically assume that you are referring to the main camera or whichever camera is currently casting to the game view when you're asking a camera's pixel resolution to be measured and converted to transform units. As a slight alteration to your answer, I declared the $$anonymous$$ain Camera (The only one I use for this project) as a Game Object (named 'mainCamera') in the touch script and I created an instance with: Camera camera = mainCamera.GetComponent(); And everything works perfectly now.

Once again thank you for your support.

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

83 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 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

Is there an easier way to do this? 1 Answer

Perspective camera touch movement 2 Answers

Free 3d Camera rotation around a 3d object with touch inputs. 0 Answers

For some reason my game fullscreens like this. How can I have my camera focus solely on the canvas? 0 Answers

Unity2D Pixel Perfect Camera: Buggy Camera 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