- Home /
What's wrong with this script?
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!
There are only 3 things that I can think of inside your code that could possibly cause a problem:
1)In Update()
, you are using Camera.main.transform, but in PickUpWeapon()
, you are using cameraVar.
$$anonymous$$ake sure that cameraVar
is assigned or just change it to Camera.main
2) In the unity scripting reference, it says that hit.collider
can be null if the raycast didn't hit anything. Try if(hit.collider == null) print("miss"); else...
3) This should be fine, but it's bugging me: you're passing the raycast into a variable ins$$anonymous$$d of directly into an if statement like I normally see. I might be optimizing the null loop here, but try passing it directly into the if and avoiding the var rayVar = ...
.
@$$anonymous$$rSplosion: Didn't you get compiler errors on that? Something like "GameObject is not part of UnityEngine.Collider".
Yes, if he assigned the var in the inspector it would work. It's not nice to use different approaches at the same time but it should work.
That's kinda pointless as long as you check if you hit something. Physics.Raycast returns a boolean that tells you that and he's checking it.
There's nothing wrong here. In this special case it's useless but in a lot cases you might need the result somewhere else.
Answer by Dreamblur · Jun 23, 2011 at 12:07 AM
These 2 lines:
if(hit.collider.GameObject.tag == "weapon"){
print(hit.collider.Gameobject.name);
should be:
if(hit.collider.gameObject.tag == "weapon"){
print(hit.collider.gameObject.name);
You could also use the CompareTag() ins$$anonymous$$d of a string comparison which is a little bit faster.
I've missed it also the first time :D but that kind of flaws are quite regular recently, so we are kinda prepared :D
As I said you should get an compiler error on this issue...
Answer by DaveA · Jun 23, 2011 at 12:08 AM
I'm guessing hit.collider.GameObject.tag should not capitalize gameObject