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;
}
}
}
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
@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;
}
}
}