• 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 ProtoTerminator · Apr 03, 2015 at 02:36 PM · nullreferenceexception

2d raycast null

 bool dropper = false;
 RaycastHit2D hit = Physics2D.Raycast(new Vector2(x-1,y), -Vector2.up, y);
 if (hit == null){
     dropper = true;
 }
 else if (hit.transform.GetComponent<pieceControls>().NormalType != NormalPieceType.Block){
     dropper = true;
 }


I'm getting a null reference exception pointing to the else if line. What's going on here? It's only supposed to go to that line if it's not null.

Comment
Add comment
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

2 Replies

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

Answer by Bunny83 · Apr 04, 2015 at 01:52 AM

You got something fundamentally wrong with Physics2D.Raycast. I admit that the way it works is a bit counter intuitive. Raycast will always return a RaycastHit2D. Since RaycastHit2D is a struct it can't be null since it's not a reference type.

Like you can see in the example of Physics2D.Raycast you should check if the collider field of the RaycastHit2D struct is null or not.

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 ProtoTerminator · Apr 05, 2015 at 09:31 AM 0
Share

Ah, ok, that makes sense. Thank you.

avatar image
0

Answer by Rednalreden · Apr 03, 2015 at 02:40 PM

It's probably the object thats hit which not contains the following script: pieceControls which will trigger a nullpointer.

Try to place a Debug line with the name of the hit object so you know what was hit with the raycast.

Comment
Add comment · Show 5 · 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 ProtoTerminator · Apr 03, 2015 at 03:09 PM 0
Share

I did that, and the only thing that's ever hit is a gameobject with that component on it.

avatar image ProtoTerminator · Apr 03, 2015 at 03:20 PM 0
Share
 RaycastHit2D hit = Physics2D.Raycast(new Vector2(x-1,y), -Vector2.up, y);
 if (hit != null){
     Debug.Log("not null, position: " + hit.transform.position);
 }



I did that, and now it's throwing the exception to the debug line. Not every time, but sometimes.

avatar image Rednalreden · Apr 03, 2015 at 03:30 PM 0
Share

If it throws an nullpointer exception on hit.transform.position the only other option is that the transform component is null. Since it's an 2D object shoudn't you get the RectTransform ins$$anonymous$$d of a transform?

avatar image ProtoTerminator · Apr 03, 2015 at 03:51 PM 0
Share

Rect is screen space. There is no RaycastHit2D.RectTransform

avatar image ProtoTerminator · Apr 03, 2015 at 06:09 PM 0
Share

Every gameobject has a transform. I don't understand what's happening here.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How could I instantiate to a GameObject variable in a class? 1 Answer

Instantiate throws NullReferenceException 1 Answer

Instantiate example gives UnassignedReferenceException error 0 Answers

Vector3.Distance giving me NullReferenceException every time 1 Answer

Attached scripts as List? 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