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
1
Question by dht1kna · Nov 30, 2010 at 01:04 PM · collisionerrorraycastnullreferenceexceptionignore

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?

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 azzogat · Nov 30, 2010 at 01:52 PM 0
Share

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?

2 Replies

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

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);
}
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 dht1kna · Dec 01, 2010 at 01:12 PM 0
Share

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

avatar image
0

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.

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 dht1kna · Nov 30, 2010 at 02:26 PM 0
Share

I get the error on this line: if(cast_col_bck.collider.gameObject.transform.parent.name == "wall")

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

No one has followed this question yet.

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


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