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
2
Question by TheKillerremijn · Apr 13, 2013 at 03:37 PM · null reference exceptionscreenpointtorayinput.mouseposition

ScreenPointToRay(Input.mouseposition) throws nullreferenceexeption

Hello, in my code here:

 using UnityEngine;
 using System.Collections;
 
 public class rotatetomouse : MonoBehaviour {
     
     public float speed = 10;
     
     // Update is called once per frame
     void Update () {
         
         Plane playerPlane = new Plane(Vector3.up, transform.position);
         
         Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition);
         float hitdist = 0;
         if(playerPlane.Raycast(ray, out hitdist)){
             Vector3 targetPoint = ray.GetPoint(hitdist);
             
             Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
             
             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
         }
         
     }
 }

line 13: Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition); throws nullreferenceexeptions about 1/3 of the time

this is the error

 NullReferenceException
 UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:376)
 rotatetomouse.Update () (at Assets/scripts/rotatetomouse.cs:13)


i personally have no idea what would be causing this, since it isnt doing it all the time i have googled around a bit for the problem but everybody else that was getting errors in this kind of statement was replaceing Camera.current.ScreenPointToRay(Input.mousePosition); with Camera.main.ScreenPointToRay(Input.mousePosition); and they didnt have a "MainCamera" in their scene

Comment
Add comment · Show 3
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 TheKillerremijn · Apr 13, 2013 at 02:51 PM 1
Share

sorry, i already fixed it, i fixed it by acctually making a main camera and using that ins$$anonymous$$d, i still find it weird that the Camera.current would be returning null when the screen is clicked though

avatar image MountDoomTeam · May 17, 2013 at 08:19 AM 0
Share

Well Spotted, it seems it's a small bug/oversight in the unity camera code.i have the same issue, it means you cant use screentopoint with 2 different cameras? perhaps i have to switch off one camera when i use the other.... yes i have to :)

avatar image Benproductions1 · May 20, 2013 at 02:11 AM 0
Share

Please close if this is answered

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by smokegun · Apr 20, 2014 at 10:40 AM

I had the same problem.

give the tag "MainCamera" to your camera!

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 Leekss · May 22, 2016 at 01:24 PM 0
Share

Omg you made me Feel stupid. $$anonymous$$y rotation finally works! Thank You.

avatar image Ranger_Dovefalcan · Sep 24, 2018 at 03:11 AM 0
Share

Thank you. I know its been 2 years since u posted this answer but you have answered my question. I have spent about an hour trying to figure out the problem.

avatar image smokegun · Sep 25, 2018 at 07:50 PM 0
Share

4 years haha.

But sure no problem guys! @Ranger_Dovefalcan @Leekss

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

17 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

Related Questions

Alternative to ScreenPointToRay needed 1 Answer

ScreenPointtoRay Creating problems for mouse follow script 1 Answer

Look at mouse cursor script not working properly 0 Answers

Ray not shooting where I click 2 Answers

ScreenPointToRay - Missing something simple here? 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