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
0
Question by lol_lol_lol · Apr 25, 2014 at 11:11 PM · 2draycastdetectiontopdown

Detect tag with 2d raycasting

So I am making a pokemon clone in unity. For this project I need to know if you can walk somewhere. I don't want to use box colliders since it looks buggy.

So I have got this so far:

         if (Input.GetKey(KeyCode.UpArrow) && isMoving == false)
         {
             bool disableMove = false;
             RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.up, 0.1F);
 
             if (hit != null)
             {
                 if (hit.collider.gameObject.tag == "collision")
                 {
                     disableMove = true;
                 }
             }
 
             if (!disableMove)
             {
                 increment = 0;
                 isMoving = true;
                 startPoint = transform.position;
                 endPoint = new Vector2(transform.position.x, transform.position.y + 1);
             }
             disableMove = false;
         }




this gives me a Nullreference on

 if (hit.collider.gameObject.tag == "collision")



I am not sure how to solve this. what am I missing?

Comment
Add comment · Show 6
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 lol_lol_lol · Apr 25, 2014 at 11:41 PM 0
Share

If you have any idea just post it. I really do need to fix this issue because at this point it's blocking my development.

avatar image getyour411 · Apr 25, 2014 at 11:46 PM 0
Share

Add debug right after hit!= null to print hit.name; I don't know how it would not be null yet return null ref on that but maybe a debug will show 'something' in hit.name

avatar image lol_lol_lol · Apr 26, 2014 at 12:15 AM 0
Share

@getyour411 there is no hit.name I have no idea how it gets past my check and then throws a nullreference.

And it does stop me from walking however not when im one tile away from the border but entirely im not able to move up anymore.

avatar image lol_lol_lol · Apr 26, 2014 at 12:19 AM 0
Share

allright so it gives me as a warning: Assets/Player$$anonymous$$ovement.cs(39,17): warning CS0472: The result of comparing value type "UnityEngine.RaycastHit2D" with null is "true" which means my hit is not null. But why am I getting a nullpointer?

avatar image robertbu · Apr 26, 2014 at 01:01 AM 1
Share

This bug has come up a number of times. I'm not sure what situations produce it or if it is by by design, but the common fix is to change line 8 to:

  if (hit.collider != null && hit.collider.gameObject.tag == "collision")
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by lol_lol_lol · Apr 26, 2014 at 12:51 AM

So i fixed it myself...

I stopped using a tag and made a new layer called it collision. then I changed this:

 RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.up, 0.1F);
  
             if (hit != null)
             {
                 if (hit.collider.gameObject.tag == "collision")
                 {
                     disableMove = true;
                 }
             }

to this:

 public LayerMask GroundLayer;
 
 
 if (Physics2D.Raycast(transform.position, Vector2.up, 1, GroundLayer))
             {
                 disableMove = true;
             }
             else
             {
                 Debug.Log("nothing");
             }

just give your gameObject a layer select the layer on your script. Then it will work!

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

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

20 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

Related Questions

Unity 2D Top-Down Raycast length/distance and direction problem 0 Answers

2d Raycast problem from enemy to player C# 1 Answer

RaycastHit2D to hit an enemy when player rotates. (Using keyboard input only) 2 Answers

Raycast AI - Avoiding Obstacles in 2D Topview (Using Velocity) 1 Answer

How can I make a raycast move towards an object? 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