NullReferenceException Please Help

I’ve found a good number of questions similar to mine, and even after reviewing those answers, my problem persists. All I’m trying to create is a method for the player to pick up and store weapons to use within their inventory, and aside from that, everything else works as intended, but as soon as the player character tries to pick up a weapon, the game window pauses and gives this error.

Here’s the PlayerController script for reference

public class PlayerController : MonoBehaviour
{
    public GameObject Player;
    public Inventory Weapons;
    public WeaponFamily pickup;

    public float turnSpeed = 20f;
    public float vel = 20f;

    Rigidbody m_Rigidbody;
    Vector3 m_Movement;
    Quaternion m_Rotation = Quaternion.identity;

    public Transform Right, Left;

    // Start is called before the first frame update
    void Start()
    {
         m_Rigidbody = GetComponent<Rigidbody>();
         Weapons = new Inventory();
   }

    // Update is called once per frame
    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        m_Movement.Set(horizontal, 0f, vertical);
        m_Movement.Normalize();

        m_Rigidbody.MovePosition(m_Rigidbody.position + ((m_Movement * vel)/10));
        m_Rigidbody.MoveRotation(m_Rotation);

        Vector3 desiredForward = Vector3.RotateTowards(transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f);
        m_Rotation = Quaternion.LookRotation(desiredForward);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            Weapons.SelectWeapon(0);
        }

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            Weapons.SelectWeapon(1);
        }

        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            Weapons.SelectWeapon(2);
        }

        if (Input.GetKeyDown("space"))
        {
            Shoot(Weapons.CurrentWeapon);
        }
    }

    void Shoot(WeaponFamily CurrentWeapon)
    {

    }

    private void OnTriggerEnter(Collider other)
    {
        pickup = other.gameObject.GetComponent<WeaponFamily>();
        if(other.CompareTag("Weapon"))
        {
            Weapons.PickupWeapon(pickup);
        }
    }
}

The last line method: the OnTriggerEnter is the area where the error lies. I’m effectively just trying to tell the game to recognize if the player comes in contact with a weapon, it performs that method PickupWeapon, which loads it into the player’s Inventory

Class for reference

public class Inventory : MonoBehaviour
{
    public PlayerController Player;

    public List<WeaponFamily> Weapons;
    public WeaponFamily CurrentWeapon;
    public int maxWeapons = 3;

    // Start is called before the first frame update
    void Start()
    {
        Weapons = new List<WeaponFamily>();
    }


    // Update is called once per frame
    void Update()
    {
        
    }

    public void PickupWeapon(WeaponFamily w)
    {
        bool inInventory = false;

        for(int i =0; i < Weapons.Count; i++)
        {
            if(w == Weapons*)*

{
inInventory = true;
}
}
if (Weapons.Count < maxWeapons && !inInventory)
{
Weapons.Add(w);

w.gameObject.SetActive(false);
w.GetComponent().isTrigger = false;

w.transform.parent = Player.transform;
w.transform.position = Vector3.zero;
w.transform.rotation = Player.transform.rotation;
}
}

public void SelectWeapon(int index)
{
if (Weapons.Count > index && Weapons[index] != null)
{
CurrentWeapon = Weapons[index];
}
CurrentWeapon.gameObject.SetActive(true);
}
}
Thanks in advance for the tips.

At line 20, you create a new instance of the Weapons variable as type Inventory. This would normally be fine, however the Inventory class inherits from Unity’s MonoBehaviour class. Because MonoBehaviours are supposed to be exclusively components attached to GameObjects, you cannot manually create an instance of a MonoBehaviour. Instead, the instance has to exist somewhere in the scene, be it on the player object or somewhere else. This is why you are getting a null reference exception - even though you attempt to create a new Inventory, it doesn’t happen by design, and thus when you try to add a weapon to it, you end up accessing an object that doesn’t exist. If you wish for Inventory to be a regular class and not directly attach it to an object in the scene, then you can change it like so;

 public class Inventory
 {
     public PlayerController Player;
 
     public List<WeaponFamily> Weapons;
     public WeaponFamily CurrentWeapon;
     public int maxWeapons = 3;
 
     public Inventory ()
     {
         Weapons = new List<WeaponFamily>();
     }
 
     public void PickupWeapon(WeaponFamily w)
     {
         ...
     }
 
     public void SelectWeapon(int index)
     {
         ...
     }
 }

Because Inventory is no longer a MonoBehaviour, functions like Start and Update no longer exist. However, you can now call the object’s constructor (like you do when you create the Weapons variable), and as such you can move your initialisation code from Start into the constructor.