var pickUpDistance : float;
var cameraVar : Transform;
private var hit : RaycastHit;
function Update(){
//debug ray
Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.TransformDirection(Vector3.forward) * 100, Color.red);
if(Input.GetButtonDown("Action")){
PickUpWeapon();
}
}
function PickUpWeapon(){
//is the object within range?
var rayVar = Physics.Raycast(cameraVar.transform.position, cameraVar.transform.forward, hit, pickUpDistance);
if(rayVar){
//is the object tagged "weapon"?
if(hit.collider.GameObject.tag == "weapon"){
print(hit.collider.Gameobject.name);
}
else{
print("not a weapon");
}
}
else{
print("miss");
}
}
I get a NullReferenceException when I press my “action” button and the object is within range. This is the script I based it off of, but instead I’m finding the tag not the name:
function Update () {
var up = transform.TransformDirection(Vector3.up);
var hit : RaycastHit;
Debug.DrawRay(transform.position, -up * 10, Color.green);
if(Physics.Raycast(transform.position, -up, hit, 10)){
Debug.Log("Hit");
if(hit.collider.gameObject.name == "floor"){
Destroy(GetComponent(Rigidbody));
}
}
}
I have no idea what I did wrong can someone help?!
Thanks!