So I’ve searched around for about 2 days and I’ve been unable to fix this issue I’ve been having. Wherein, I receive a Null Reference Exception when I try to check the tag of a raycast hit target on the ground. Here’s a brief explanation of what I’m trying to do.
var hit : RaycastHit;
function Update() {
Debug.DrawRay (transform.position, transform.forward * 10, Color.green);
if (Physics.Raycast (transform.position, transform.forward, 10)) {
if(hit.collider.tag == "Pickup"){
print("Pickup!");
}
else{
return hit;
}
}
}
So essentially, my raycast is labeled here by the green Debug.DrawRay. The raycast shoots from the center of my flashlight beam and attempts to detect any items with the tag “Pickup” within a distance of 10 units from the player. However, when I attempt to ask about the tag using an if statement, I am uncertain what to use in order to ask the proper question. hit.collider.gameObject.tag or hit.collider.tag. I’ve also tried hit.transform.gameObject.tag and hit.transform.tag. All of which give me the same error. Which begs the question, am I missing something or am I going about this entirely wrong?
NullReferenceException: Object reference not set to an instance of an object
pickup_objects.Update () (at Assets/pickup_objects.js:4)