• 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 unity_9ryVQ-86xltNEA · Jun 11, 2021 at 03:05 PM · raycastgetcomponentnull reference exception

groundcheck function returns a NullReferenceException error when the player is airborne

So i have this ground check function. It uses a boxcast to check if the object its touching has a componenet called PlayerGround which decides what is ground and what isn't. It works, but when my player is airborne I get an error.

This is the error given: NullReferenceException: Object reference not set to an instance of an object

This isn't a big deal but I would like for my console to not be filled up by these. My code is below.

     groundCheckData = Physics2D.BoxCast(transform.position - new Vector3(0, 0.66f), new Vector2(0.99f, 0.3f), 0, Vector2.down, 0);

     if (groundCheckData.collider.gameObject.GetComponent<PlayerGround>() != null)
     {
         Grounded = true;
     }
     else
     {
         Grounded = false;
     }
Comment
Add comment · Show 1
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 unity_HTr7mOeTIRzvVA · Jun 11, 2021 at 03:47 PM 0
Share

Check if there's a ground collider first, then you can go to the game object to get the PlayerGround component. Maybe it is saying Null because your trying to get a PlayerGround component from the collider that doesn't exist during airborne.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RLin · Jun 11, 2021 at 04:44 PM

The problem is this line:

 if (groundCheckData.collider.gameObject.GetComponent<PlayerGround>() != null)

To evaluate the if statement, it tries to evaluate groundCheckData, which works. Then, it tries to evaluate groundChecKData.collider; since you're not touching the ground, this returns null, but there aren't any errors yet. Finally, when groundCheckData.collider.gameObject is evaluated, this is trying to find the gameObject attached to null since groundCheckData.collider was null, so you get an error.

Something you could do is replace your if statement with this:

 if (groundCheckData.collider && groundCheckData.collider.GetComponent<PlayerGround>())

Boolean evaluations in C# implement short circuiting, which means that if we can know the result of the boolean without evaluating the entire expression, it will stop early. What this means is that if groundCheckData.collider is null, it doesn't even bother evaluating the GetComponent, so you won't run into any NullReferenceExceptions. As a final note, while it isn't necessary to solve the error you're getting, using GetComponent() every frame can be very performance intensive. Consider tagging your ground colliders instead of giving them a component, and then just checking the tag instead.

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 unity_9ryVQ-86xltNEA · Jun 11, 2021 at 05:04 PM 0
Share

If i am to use tags is there a way to add multiple tags to one object?

avatar image RLin unity_9ryVQ-86xltNEA · Jun 11, 2021 at 05:12 PM 0
Share

With the default tag setup that is not possible, but there are a number of workarounds.

As a matter of fact, I realized that a better solution would be to use LayerMasks. Create a layer called PlayerGround, and then have the BoxCast only cast against the PlayerGround layer; this way, you know for sure that the results of the BoxCast are always going to be the ground.

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

172 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

How do I get my Fireball toward my mouseposition?(some non working tries example in here :3) 1 Answer

GetComponent, gameObject.tag vs gameObject.layer 1 Answer

How can I pre-load all the possible components to avoid stuttering with GetComponent? 0 Answers

Null Reference Exception Help 1 Answer

GetComponent Picking up multiple scripts from differrent objects? 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