- Home /
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;
}
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.
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.
If i am to use tags is there a way to add multiple tags to one object?
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
Follow this Question
Related Questions
Why isn't this raycast working? 2 Answers
().enabled=false stopped working 1 Answer
using Physics.Raycast to run functions on diffrent gameObjects? 1 Answer
getting position from a script 2 Answers
GetComponent null reference C# 1 Answer