The following code is being used in an RTS style game to detect when a unit comes within a certain distance of a crate to allow them to ‘pick up’ objects. However, every time a unit enters the collider around the object the script is attached to, it merely throws up a NullReferenceException and nothing happens (the message sent out should stop the light attached to the object and destroy it).
using UnityEngine;
using System.Collections;
public class PickupOnCollider : MonoBehaviour {
void OnTriggerEnter (Collider other)
{
GameObject enteringObject = other.transform.parent.gameObject;
Debug.Log("Object entering collider...");
if (enteringObject.light != null)
{
if (enteringObject.light.tag == "Allied")
{
BroadcastMessage("OnPickup");
Debug.Log("Item picked up.");
}
}
}
}