Question by
River-Ofarrows · Dec 13, 2015 at 07:38 PM ·
error messageraycasting
Object referance not set to an instance of an object?
I am using a raycast script but it says "Null Referance Exeption Object referance not set to an instance of an object", and doesn't print anything How do i fix this
function Update () {
var foundHit : boolean = false;
var hit : RaycastHit;
if (Input.GetButton("Fire1")){
if (Physics.Raycast(transform.positon, transform.forward))
print("YES");
else
print("NO");
}
}
Comment
Answer by JedBeryll · Dec 13, 2015 at 08:27 PM
You are not hitting anything. I don't think raycasthit can be converted to boolean so you are passing null to an if statement where it expects bool. First cast your ray, check for input, check if hit.collider != null.