0

Hello so I am new and trying to have it so that when an object is collided they will disappear and be added into an inventory. I am getting this error when I run and collide with the item I want to pick up, :NullReferenceException: Object reference not set to an instance of an object at MovementScript.AddToInventory (UnityEngine.Transform transform) (at Assets/Scripts/MovementScript.cs:30) and MovementScript.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/MovementScript.cs:26) so at line 30 and 26 I messed up with the code but looking back I thought I referenced everything. Can someone with a more experienced eye tell me where i went wrong? here is my code

    void OnTriggerEnter(Collider other) {
        AddToInventory(other.transform.parent);
    }
    public void AddToInventory(Transform transform)
    {
        inventory.items.Add(transform);
        transform.gameObject.SetActive(false);
    

The error says it is happening at this line AddToInventory(other.transform.parent); and this line inventory.items.Add(transform);

Gabe S
  • 1
  • 1
  • 1
    what if the object you collided with has no parent object ...? Otherwise are you sure that `inventory` and `inventory.items` are both initialized correctly? – derHugo Feb 28 '22 at 07:46

0 Answers0