Error (Object Reference Is Required)

I have a collider cube with a rigidbody that I’ve parented to my player object. The guns on the player object can aim in all sorts of ways but sometimes the bullets they shoot can collide with the player object’s collider itself.

The bullets have their own script that use a raycast to tell if they should come into contact with an object, but when I try to tell the raycast not to activate if the object is the player collider object it gives me that error. Here is what my code looks like:

using UnityEngine;
using System.Collections;

public class MF_BasicProjectile : MonoBehaviour
{
   public GameObject jet_rigid_body;

   void FixedUpdate ()
   {
     RaycastHit hit = default(RaycastHit);
    
     if ( Physics.Raycast(transform.position, myRigidbody.velocity, out hit, myRigidbody.velocity.magnitude * Time.fixedDeltaTime ) )
     {
       if (RaycastHit.transform.gameObject != jet_rigid_body)
       {
         Destroy(gameObject);
       }
     }
   }
}

Currently I have dragged the ‘jet_rigid_body’ prefab into the inspector, but it gives me this error.
This is the line it doesn’t seem to like:
if (RaycastHit.transform.gameObject != jet_rigid_body)

Thank you very much for your time-

you’re trying to use the class “RaycastHit” as if you are working with a static attribute/function… you want

hit.transform.gameObject

i.e. this specific instance of RaycastHit

1 Like

Thank you very much. That helps a lot!