Everytime the raycast hits a collider within 1.5f I get the error object reference not set to an instance of an object. Anyone have any help?
void Items() {
if(Physics.Raycast(gameObject.transform.position, gameObject.transform.TransformDirection(Vector3.forward), 1.5f))
print(ray.collider.gameObject.name);
}
I’m assuming that ‘ray’ is a RaycastHit. Typically the name ‘hit’ is used and the name ‘ray’ is saved for variables of type Ray. The reason you are getting a null reference exception is that you are not passing ‘ray’ as a parameter to the Raycast(), and therefore it is not being assigned a value. Try:
if(Physics.Raycast(gameObject.transform.position, gameObject.transform.TransformDirection(Vector3.forward), 1.5f, ray))