• 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 Globalgig · Oct 29, 2014 at 03:01 AM · c#nullreferenceexceptiontagsrays

RaycastHit2D not interacting with tags

I've been hacking my way through references and other questions, but I haven't stumbled upon an answer that has helped a whole ton. I've tried to create a ray that projects from the position of the mouse, and that ray is supposed to 'pick up' any tags that it collides with. On line 19 (not including the part that wasn't transmitted into the code), it says "NullReferenceException: Object reference not set to an instance of an object" and I'm quite new to unity, so I'm not entirely sure how to interpret this... If anyone could help with my little blip, that would be greatly appreciated!

using UnityEngine; using System.Collections;

public class MousePosition : MonoBehaviour {

 private Vector2 ScreenPosition;

 public bool MouseOnTent = false;
  
 void Start () {
 }

 void Update () {
     MouseInteraction ();
 }

 void MouseInteraction() {
     ScreenPosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
     RaycastHit2D hit = Physics2D.Raycast (ScreenPosition, Vector2.zero);
     //Creates a ray that detects if it is hitting a tag where the mouse is


     if (hit != null) {
         if (hit.transform.tag == "Tent")
             MouseOnTent = true;
     }
     else {
         MouseOnTent = false;
     }
 }

}

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
0

Answer by robertbu · Oct 29, 2014 at 03:03 AM

For future questions please format your code. After pasting, select your code and use the 101/010 button. Also for future questions include the full text of the error message from the console. Without these two, I'm left guessing about the error. I'll be it is this line:

  ScreenPosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));


'camera' will refer to any camera on the same game object with this script. If this script is not attached to a camera, you'll get a null reference exception. You may want to replace 'camera' with 'Camera.main'. 'Camera.main' will be the camera in the scene with the tag 'MainCamera', so it too will fail if you change the tag on the default camera Unity puts in a scene.

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.

Welcome to Unity Answers

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.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Strange Problem: NullReferenceException 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Accessing Instance Bool from other script 2 Answers

C# problem something in hand 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges