• 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 MarcosLC · Dec 07, 2013 at 07:33 PM · physics.raycast

NullReferenceException with Physics2D raycast

Hi, I'm getting a error with raycasting (Physics 2D). I have this code in a Gun, but I get a NullReferenceException (Object reference not set to an instance of an object)...

This is my script:

 using UnityEngine;
 using System.Collections;
 
 public class GunRaycast : MonoBehaviour {
 
     public Transform gunPoint;
     public Transform mirilla;
 
     public Vector2 mousePos2D;
 
     public Gun gun;
 
     void Awake(){
         gun = GetComponent<Gun>();
     }
 
     void Update () {
         mousePos2D = new Vector2(gun.screenPos.x, gun.screenPos.y);
 
         RaycastHit2D hit = Physics2D.Raycast(gun.transform.position, mousePos2D);
 
         if(hit.collider.gameObject.name == "PhysicableSquare"){
             Debug.Log ("PhysicableSquare hit");
         }
     }
 }


I don't know what I'm doing bad, so, Can someone explain me how?

Thanks.

Here a image of the structure of my game:

structure

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 meat5000 ♦ · Dec 07, 2013 at 08:04 PM 0
Share

What line is the error on?

avatar image Cabooska · Dec 16, 2013 at 07:33 AM 0
Share

I'm having the same issue. Have you made any progress with this? It works just fine when not using physics2D.

avatar image robertbu · Dec 16, 2013 at 07:41 AM 0
Share

In my tests, I'm getting back non-null values for the 'hit' in situations where the raycast cannot succeed. In those cases, the hit.collider is null. I'm not sure if this is a Unity bug, or if there is something fundamental I'm missing about Physics2D.Raycast(). A workaround is to check both 'hit' and the 'collider'.

 if (hit != null && hit.collider != null)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ziv03 · Dec 07, 2013 at 07:42 PM

You're getting the NullReferenceException because the raycast returns null when there is no collider in the direction. you can do this:

   if(hit != null)
   {
       if(hit.collider.gameObject.name == "PhysicableSquare"){
           Debug.Log ("PhysicableSquare hit");
       }
   }
Comment
Add comment · Show 2 · 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 MarcosLC · Dec 07, 2013 at 07:58 PM 0
Share

I'm still getting the error :S

avatar image ziv03 · Dec 07, 2013 at 11:24 PM 0
Share

sorry, i edited it. hit != null and not hit != nul

avatar image
2

Answer by idbrii · Sep 27, 2014 at 03:29 PM

The RaycastHit2D's collider documentation says you can check the result directly (which uses the "implicit conversion operator converting to bool"):

 RaycastHit2D result = Physics2D.Linecast(start, end, seeTargetMask);
 if (result)
 {
     // Find something we can hurt.
     Kill(result.transform.gameObject);
 }

I think that's clearer than checking the collider.

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 courtesy · May 13, 2020 at 03:02 AM 0
Share

I don't get what idbrii means by "you can check the result directly" but where if (raycastHit != null) {... failed for me if (raycastHit) {... worked

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

21 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

Related Questions

Raycast distance affected by momentum of character 0 Answers

problem with dead space kinesis module system 1 Answer

Is there a way to use Physics.Raycast accurately while time.timescale is set to zero? 1 Answer

physics.raycast nullReferenceException 1 Answer

Change game-cursor on gameobjects within range 0 Answers

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