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.