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

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

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

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

Using raycast to determine if player is grounded. 1 Answer

RaycastHit2D problem with PolygonCollider2D 2 Answers

Raycasting only works when close to an object 0 Answers

Physics.Raycast If/Else Problem 0 Answers

Physical shots 3 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