OnTriggerEnter Null Object Reference

So I was making an object pickup script for VR, and I was almost done patching all possible bugs it had.
I was adding a new handler for this bug where if my right controller was holding an object, and I put my left controller into it, it would drop it’s object. I just finished the script and decided to test it out. The right controller worked, but when my left controller collided with an object, I got this error:

Object reference not set to an instance of an object Pickup.OnTriggerEnter (UnityEngine.Collider other)

And this was an internal error it seems. Takes place at line 46:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pickup : MonoBehaviour
{
    public bool canHold;
    public bool isHolding;
    public GameObject PickableObject;
    public Pickup Left;
    public Pickup Right;
    public bool isLeft;

    public void Down()
    {
        if (canHold == true)
        {
            if (PickableObject != null)
            {
                if (isHolding == false)
                {
                    isHolding = true;
                }
                else if (isHolding == true)
                {
                    isHolding = false;
                }
            }
        }
    }

    void Start()
    {
        if (gameObject.name == "XRControllerLeft")
        {
            isLeft = true;
        }
    }

    public void OnTriggerEnter(Collider other)
    {
        if (PickableObject == null)
        {
            if (isLeft == true)
            {
                if (other.gameObject != Right.PickableObject)
                {
                    if (isHolding != true)
                    {
                        if (other.gameObject.GetComponent<PickableObjectData>())
                        {
                            canHold = true;
                            PickableObject = other.gameObject;
                        }
                    }
                }
            }
            else if (isLeft == false)
            {
                if (other.gameObject != Left.PickableObject)
                {
                    if (isHolding != true)
                    {
                        if (other.gameObject.GetComponent<PickableObjectData>())
                        {
                            canHold = true;
                            PickableObject = other.gameObject;
                        }
                    }
                }
            }
        }
    }
    public void OnTriggerExit(Collider other)
    {
        if (isHolding != true)
        {
            canHold = false;
            PickableObject = null;
        }
    }
    void Update()
    {
        if (PickableObject != null)
        {
            if (isLeft == true)
            {
                if (isHolding == true)
                {
                    PickableObject.transform.parent = transform;
                    PickableObject.GetComponent<Rigidbody>().isKinematic = true;
                }
                else if (isHolding == false)
                {
                    PickableObject.transform.parent = null;
                    PickableObject.GetComponent<Rigidbody>().isKinematic = false;
                }
            }
            else if (isLeft == false)
            {
                if (PickableObject != Left.PickableObject)
                {
                    if (isHolding == true)
                    {
                        PickableObject.transform.parent = transform;
                        PickableObject.GetComponent<Rigidbody>().isKinematic = true;
                    }
                    else if (isHolding == false)
                    {
                        PickableObject.transform.parent = null;
                        PickableObject.GetComponent<Rigidbody>().isKinematic = false;
                    }
                }
            }
        }
    }
}

Any advice on other things in this code is much appreciated.
Thanks in advance.

Edit: I fixed it because Right wasn’t assigned in the inspector, but for some reason it was saying that Collider other wasn’t defined.

This is just a null ref error.

  1. Find out what is null
  2. Find out why it’s null
  3. Fix it.

So, looking at line 46, my guess is Right is null.

1 Like

But how does it trigger without that being defined?

Well nothing assigns Right in your code, so it’s most likely you never assigned it in the inspector?

It’s just a simple null ref. Figure out what is null, stop it being null.

1 Like

I assigned it in the inspector. Everything that should be assigned is assigned.

Edit: I thought Right was assigned in the inspector.

I fixed it because Right wasn’t assigned in the inspector, but for some reason it was saying that Collider other wasn’t defined.

No it wasn’t, it was just telling you there was a null ref on a particular line. And because ‘nothing’ can’t cause a collision, there was only one possibility: that Right was null.

1 Like