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
0
Question by laurenmhowie · Jul 08, 2018 at 05:54 PM · raycast2d gameraycasting2d-physicsraycasthit

Problems with 2D RaycastHit.Collider

Hello all,

I can't seem to figure out why Unity is giving me a "Null Reference Exception" saying "the Object Reference not set to an instance of an object" for line 29. This is my frist time trying to work with a ray cast, in the API it seemed to me that after your ray cast has hit a collider it could return info from that game object. Why can't I access the transform of the object my ray cast is hitting? Have I written something wrong? I also have had a horrible time in the past trying to write a "pick up and carry" script for my game so I intended for this to be the beginning of another attempt at that ! Thanks in advance for time and for reading!

  using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class PlyrgrabThrow : MonoBehaviour {
     
         public Transform PlyrStart, PlyrEnd;
         public bool close = false;
         public GameObject holdpoint;
         public RaycastHit2D rayCast2D;
         void Update()
         {
             Raycasting ();
             //Behaviors();
         }
     
         void Raycasting()
         {
             Debug.DrawLine(PlyrStart.position,PlyrEnd.position,Color.green);
             close = Physics2D.Linecast (PlyrStart.position, PlyrEnd.position, 1 << LayerMask.NameToLayer("Grabbed"));
         }
     
     
     
         void Behaviors(){
     
             if (close == true && Input.GetKeyDown(KeyCode.X)){
     
                 rayCast2D.collider.transform.position = holdpoint.transform.position;
             }
         }
     }
     
     
     
         
 
     

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

Answer by RustyCrow · Jul 08, 2018 at 09:13 PM

I might be misunderstanding your question, but if your are trying to get information from your Physics2D.Linecast you are returning it into a Bool not into your RaycastHit2D rayCast2D, so its (possibly?) empty, unless you are doing som other stuff in the background.

RaycastHit2D = Physics2D.Linecast (Sends data of what it hit TO Raycasthit2d) ---> RaycastHit2D.Transform (transform of whatever you hit)

So what is most likely NULL is your rayCast2D because your not doing anything with it.

hope this sheds some light on what may be the problem @laurenmhowie

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
avatar image
0

Answer by laurenmhowie · Jul 08, 2018 at 09:24 PM

@RustyCrow Thanks for you response! I just found a video where someone was doing something similar and the new way I've put this together is allowing me to use the functionality in Raycasthit2D, I still haven't figured out the best way to parent my first game object's transform to a second one, which doesn't seem like it should be such a hassle but whew at least this is seeming resolved. So Thank you for the reply!

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class PlyrgrabThrow : MonoBehaviour {
 
     public Transform PlyrStart, PlyrEnd;
     public bool close = false;
     public GameObject holdpoint;
     public RaycastHit2D rayCast2D;
 
 
 
 
     
     void Update()
     {
         Raycasting ();
         Behaviors();
     }
 
     void Raycasting()
     {
         Debug.DrawLine(PlyrStart.position,PlyrEnd.position,Color.green);
 
         if(Physics2D.Raycast (PlyrStart.position, PlyrEnd.position, 1 << LayerMask.NameToLayer("Grabbed"))){
             rayCast2D = (Physics2D.Raycast (PlyrStart.position, PlyrEnd.position, 1 << LayerMask.NameToLayer("Grabbed")));
                 close = true;
     }
                 else
                 {
                     close = false;
                 }
                 }
 
 
 
     void Behaviors(){
 
         if (close == true && Input.GetKeyDown(KeyCode.X)){
 
         rayCast2D.collider.transform.position = holdpoint.transform.position;
         }
     }
 }
 
 
 
     
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 RustyCrow · Jul 08, 2018 at 09:51 PM 0
Share

No problem, remember to mark this as solved and to parent --> Transform.parent = gameobject you want to child. @laurenmhowie

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

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

Is this way of making a raycast correct 2 Answers

¿Why Raycast sometimes don't collide with rigidbody (plus collider)? 0 Answers

Raycast returns null for no apparent reason 0 Answers

Raycast not hitting instantiated object when timescale is 0 1 Answer

Rigidbody.position causes shaking 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