- Home /
Question by
JackBid · May 29, 2013 at 02:37 PM ·
nullreferenceexceptionphysics.raycast
physics.raycast nullReferenceException
i get this error when using this raycast code:
var back = transform.TransformDirection (Vector3.back);
var hit : RaycastHit;
if (Physics.Raycast (transform.position, back, 10)) {
print (hit.collider.tag);
}
the error specifically is :
NullReferenceException: Object reference not set to an instance of an object.
the object it hits is a prefab which has been instantiated and has a box collider.
Comment
Answer by Dave-Carlile · May 29, 2013 at 03:12 PM
Your hit
variable isn't set to anything. You need to call one of the Physics.Raycast methods that returns the hit info.
Your answer

Follow this Question
Related Questions
Weird NullReferenceException 2 Answers
Arraylist to GameObject[] array issue. 1 Answer
[Roll-a-ball] Changing UI text (NRE) 0 Answers