Search Unity

Weapon Issues - RaycastHit.rigidbody causing an error

Discussion in 'Scripting' started by StarManta, Oct 24, 2006.

  1. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    (First off, as a side note, can a developer please allow copying out of the error console?)

    OK. I've got a ray being cast to represent a gun firing. Everything about this ray is working well - it hits the proper position, creates a spark where it's supposed to.

    I added the following:
    if (BHit.rigidbody) {
    BHit.rigidbody.AddForceAtPosition(Vector3.Normalize(target.position - transform.position)*Force, BHit.point);
    }

    Whenever the gun hits an object with a rigidbody, I get the error: NullReferenceException: Object reference not set to an instance of an object

    This error points to the line containing "if (BHit.rigidbody) {"

    My code, as far as I can discern, is conceptually identical to the code used to accomplish the same effect in the FPS tutorial, and I'm stumped as to what this error means and why it's coming up. Does anyone have any ideas?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You can use the system wide console for copy and paste.
    (/Applications/Utilities/Console.app)

    Can you post a wider context of what you are trying to do. Maybe the full script.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Alright.

    The script works in conjunction with another script that puts the target object (which represents a lasersight) where the player is aiming. It casts a ray in the direction of that object. It creates a spark, and, if the object it hits is a rigidbody, applies some force to it....in theory. Then it plays a sound.

    The rigidbody part is the only malfunctioning part of this script.

    Code (csharp):
    1. var target :Transform; //this should be a lasersight object as used in MouseAim
    2. var SparkObject : GameObject;
    3. var Force = 25.0;
    4. var audioSource : AudioSource;
    5. var SoundEffects : AudioClip[]; //picks one of these effects at random
    6.  
    7. function Update () {
    8.     if (Input.GetMouseButtonDown(0)) {
    9.         var BHit : RaycastHit;
    10.    
    11.         Physics.Raycast(transform.position, (target.position - transform.position), BHit);
    12.        
    13.         var s = Instantiate(SparkObject, BHit.point, Quaternion.LookRotation(BHit.normal));
    14.         Destroy(s, 0.5);
    15.        
    16.         if (BHit.rigidbody) {
    17.             BHit.rigidbody.AddForceAtPosition(Vector3.Normalize(target.position - transform.position)*Force, BHit.point);
    18.         }
    19.        
    20.         //AUDIO
    21.         if (SoundEffects.length > 0) {
    22.            
    23.             var thisSound = SoundEffects[Random.Range(0,SoundEffects.length)];
    24.             audioSource.PlayOneShot(thisSound);
    25.  
    26.         }
    27.     }
    28. }
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    maybe target.position points to nothing?

    What's the error you are getting?
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    No, the error occurs on the line with the if statement - even if the AddForceAtPosition line is commented out, I still get the error.
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Probably. What error is it giving you? (Use the system console to copy-paste)
     
  7. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    If you comment out the entire if statement do you still get the error?
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I think I've found the source of the issue, though according to the documentation it should work fine. Anytime I touched BHit.transform, even in
    Code (csharp):
    1.  if (BHit.transform)
    , it would trigger this error. I've worked around it by using BHit.collider instead.

    BHit.rigidbody seems to be problematic too, so I started using BHit.collider.attachedRigidbody. My code now looks like
    Code (csharp):
    1.  
    2.             if (BHit.collider.attachedRigidbody) {
    3.                 BHit.collider.attachedRigidbody.AddForceAtPosition(Vector3.Normalize(target.position - transform.position)*Force, BHit.point);
    4.             }
    5.  
    It no longer produces an error. However, it never adds the force to the object, as far as I can tell. It's getting inside the if statement (tested with a print statement), so the lights are on, but nobody seems to be home.
     
  9. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You are not checking for the return value of the raycast.

    Physics.Raycast(transform.position, (target.position - transform.position), BHit);

    Raycast returns you a bool if it has hit anything. If it hits nothing false is returned. You shouldn''t access the raycasthit if nothing is hit. The collider is always null in this case. So when you access the rigidbody instead of the collider you get an exception. I'll change the behaviour of this to return null instead of an exception for 1.6 but you shouldn't access the raycast hit struct if you didn't hit anything in any case.