x


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!

more ▼

asked Jun 22 '11 at 11:53 PM

MrSplosion gravatar image

MrSplosion
132 45 52 61

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. Make 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 instead 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 = ....

Jun 23 '11 at 12:23 AM SilverTabby

@MrSplosion: Didn't you get compiler errors on that? Something like "GameObject is not part of UnityEngine.Collider".

@SilverTabby:

  1. 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.
  2. 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.
  3. There's nothing wrong here. In this special case it's useless but in a lot cases you might need the result somewhere else.
Jun 23 '11 at 03:19 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);
more ▼

answered Jun 23 '11 at 12:07 AM

Dreamblur gravatar image

Dreamblur
703 9 16 22

Ya beat me to it!

Jun 23 '11 at 12:08 AM DaveA

You could also use the CompareTag() instead of a string comparison which is a little bit faster.

Jun 23 '11 at 12:16 AM save

Ok thanks I cant belive u spotted that!

Jun 23 '11 at 12:19 AM MrSplosion

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...

Jun 23 '11 at 03:23 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

I'm guessing hit.collider.GameObject.tag should not capitalize gameObject

more ▼

answered Jun 23 '11 at 12:08 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x181
x33

asked: Jun 22 '11 at 11:53 PM

Seen: 913 times

Last Updated: Jun 23 '11 at 03:23 AM