I have a problem with ScreenToWorldPoint because it just spews out "NullReferenceException: Object reference not set to an instance of an object.""
1st way i used it:
Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
2nd way i used it:
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Can you please help me with this problem? Thanks!
Answer by IgorAherne
Mar 18 at 01:04 AM
2nd way is correct.
You get Nullref because Camera.main is null
Nullref is basically when you trying to use something from non-existing object, in your case Camera.main
Camera.main is automatically assigned by unity. Just make sure there is a camera gameObject in your scene with a tag "MainCamera"
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
113 People are following this question.
Player Rotation Around Mouse
Placing and rotating dominoes?
Unity lookat at finger position 3D C#
Rotating a 2D object to look at mouse position is not entirely accurate
How to Set a Single Axis Rotation of a GameObject?