- Home /
Ignore NullReferenceException error?
I get a nullref. error when i use a raycast which checks if it is colliding with a wall.
Here is the code:
if (Physics.Raycast(transform.position,Vector3(0,0,-1),cast_col_bck,ray_dist_bck)) {
if(cast_col_bck.collider.gameObject.transform.parent.name == "wall")
{
Destroy(gameObject);
}
}
I think this happens when the raycast is not colliding with anything. If that is the case i dont really care about the error is there a way to prevent this?
It's surely not about the raycast not colliding. NUllReferences usually mean that certain objects you're referencing (GOs, scripts, etc) aren't there or that you've misspelled them. On which line do you get the error?
Answer by skovacs1 · Nov 30, 2010 at 04:06 PM
Your problem is this:
if(cast_col_bck.collider.gameObject.transform.parent.name == "wall")
- cast_col_bck - the RaycastHit from the Raycast and is guaranteed to be set.
- .collider - the collider of the object that was hit and because the raycast hit, it is also guaranteed to be set.
- .gameObject - every component must have a GameObject so this is also guaranteed to be set.
- .transform - every GameObject has a Transform so this is guaranteed to be set.
- .parent - This may or may not exist.
- .name - every GameObject has a name, even if it is an empty string.
The object you hit doesn't have a parent, you can ignore the exception, but the best solution is to not generate one. Also, you may not realize this, but RaycastHit has a transform, meaning that you can save yourself several of your references by using cast_col_bck.transform.parent.name
.
To avoid your exception causing condition, you would simply check for the thing that is causing your exception like so:
if(Physics.Raycast(transform.position,Vector3(0,0,-1),cast_col_bck,ray_dist_bck)
&& cast_col_bck.transform.parent
&& cast_col_bck.transform.parent.name == "wall") Destroy(gameObject);
or better still, if you know that your "wall" is the root of the hierarchy that you are looking for, use Transform.root which will never return null like so:
if(Physics.Raycast(transform.position,Vector3(0,0,-1),cast_col_bck,ray_dist_bck)
&& cast_col_bck.transform.root.name == "wall") Destroy(gameObject);
To ignore an exception, you would do something like
try{
somethingDangerous();
}
catch(e : Exception) {
Debug.LogError(e.Message);
}
Thanks :). I did not know that you can check if a object has a parent by just using transform.parent, I learned something new :D
Answer by pyro · Nov 30, 2010 at 01:52 PM
If the raycast wasn't colliding with anything then Physics.Raycast would return false and it wouldnt even hit the nested if statement.
If I had to guess based off this code snippet.. probably whatever your Raycast is hitting, you are then going out and trying to grab its parent which it might not have and thats why you got the null reference error.
Anyways, in order to ignore exceptions you need to "catch" them by setting up try/catch blocks and handle them there.
I get the error on this line: if(cast_col_bck.collider.gameObject.transform.parent.name == "wall")
Your answer
Follow this Question
Related Questions
Raycast doesn't collide as it should 1 Answer
Using GetComponent Within OnTriggerEnter 0 Answers
(2D) Raycast check - null reference even when (RayHit != null) 2 Answers
Raycast sometimes ignore objects. 1 Answer
raycast shooting 0 Answers