Object reference not set to an instance of an object!

So, I’m trying to make a script that can get the script called WeaponBehaviour from the object that you collide into, and then make a copy of it under the transform Primary (I’m just using a temporary weapon because I couldn’t get the copying part of it right). But unity is giving me an error saying that Object reference not set to an instance of an object. I have no idea how to fix and it’ll be great if you do! Thanks.
public Transform primary;
public Transform tempweapon;
private MonoBehaviour otherWeapon;
public bool havePrimary = false;

    public void OnTriggerEnter(Collider other)
    {
        Debug.Log("weapon detected");
        if (other.transform.tag == "Primary" && !havePrimary)
        {
            Debug.Log("weapon detected");
            otherWeapon = other.GetComponent<WeaponBehaviour>(); // This is the alternative that didn't work
            other.GetComponent<WeaponBehaviour>().dropped = false; //This is were the error is
            Instantiate(tempweapon, weaponHold.position, weaponHold.rotation, primary);
            havePrimary = true;
        }
    }

And the weapon script with the parent script as well:

public class BlockGunMk1_Behaviour : WeaponBehaviour
{
    weaponTypes BlockGunType;
    public Rigidbody rigid;
   

    // Use this for initialization
    public override void Start()
    {
        BlockGunType = weaponTypes.Primary;
        base.Start();
    }

    // Update is called once per frame
    public override void Update()
    {
        if (!dropped)
        {
            rigid.isKinematic = true;
            rigid.detectCollisions = false;
        }
        else
        {
            rigid.isKinematic = false;
            rigid.detectCollisions = true;
        }
        base.Update();
    }
}

Parent

[RequireComponent(typeof(Rigidbody))]
public class WeaponBehaviour : MonoBehaviour {

    protected Rigidbody rigid;
    public static float pickupDistance = 2f;
    public bool dropped = true;
    protected enum weaponTypes{Primary, Secondary, Melee};

    // Use this for initialization
    public virtual void Start () {
        rigid = GetComponent<Rigidbody>();
    }
   
    // Update is called once per frame
    public virtual void Update () {

    }
}

And here the error:
NullReferenceException: Object reference not set to an instance of an object
Unit_PlayerBehaviour.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Script/Player/Unit_PlayerBehaviour.cs:115)

If you are getting a null reference error when using GetComponent then it didn’t find the script WeaponBehaviour on the “other” object.

1 Like

Thanks for replying, it works now!