Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Reedex · Apr 19, 2018 at 11:30 AM · raycasthit2dobject reference

hi,Why the raycast is having object reference error on the : "if (hit.collider.gameObject.tag == "Player") " line? thanks

 void Start () {
         player = GameObject.Find("Player").transform;
     }
 
     // Update is called once per frame
     void Update () {
 
         RaycastHit2D hit = Physics2D.Raycast(gameObject.transform.position,player.transform.position, 100f);
         Debug.DrawLine (gameObject.transform.position,player.transform.position, Color.cyan);
 
         if (Vector2.Distance (transform.position, transform.forward) < shootdist) 
         {
             transform.LookAt (player.position);
             transform.Rotate (new Vector3 (0, -90, 0), Space.Self);
 
             if (hit.collider.gameObject.tag == "Player") 
             {
                 
                 nextshoot -= Time.deltaTime;    
             }
         }
 
         if (nextshoot < 0) 
         {
             nextshoot = timebetweenshots;
             Rigidbody clone;
             clone = Instantiate (projectile, transform.position, transform.rotation) as Rigidbody;
         }
 
     }
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
2
Best Answer

Answer by Bunny83 · Apr 19, 2018 at 12:27 PM

The 2d raycast works different from the 3d raycast. While the 3d raycast returns true or false whether there's a hit or not, the 2d version always returns a "RaycastHit2D" struct, even when there's no hit at all. That's why, if you care to check the example in the docs, you have to check if the hit struct actually contains a valid collider reference or not. That means if there's no hit at all the collider reference in your "hit" will be null.

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 Reedex · Apr 19, 2018 at 08:33 PM 0
Share

yes i needed condition --> if hit.collider != null...thanks again b

avatar image
0

Answer by Vollmondum · Apr 19, 2018 at 11:35 AM

Assign Player as public variable instead of finding it, or add if(player != null), otherwise you're checking null object, if Find line failed

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 Reedex · Apr 19, 2018 at 11:42 AM 0
Share

i got it serialised,its a find, but, for some reason if i stand on the right side of turret, it shoots properly

avatar image Vollmondum Reedex · Apr 19, 2018 at 11:46 AM 0
Share

Sounds like my ex-issue, the 2d game should be in one axis coordinate segment. say all X and Y should be > 0. $$anonymous$$ove everything away from 0 coordinate into one quarter

avatar image Reedex Vollmondum · Apr 19, 2018 at 08:35 PM 0
Share

yes that changed behaviour too, although ,it wasn't the issue now, i sure 'll keep that in $$anonymous$$d thanks for the heads up :)

Show more comments

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

78 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 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 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 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

Using methods from other classes on a linecast hit 0 Answers

C#, "object reference not set to an instance of an object" 3 Answers

NullReferenceException: Object reference not set to an instance of an object 1 Answer

object reference not set to an instance of an but works with other script 1 Answer

MenuScreenController.cs(2,19): error CS0234: The type or namespace name `SceneManagement' does not exist in the namespace `UnityEngine'. Are you missing an assembly reference? 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