Unity3d noob here. Was trying to get proximity detection (polling every frame/update) working when the FPS camera gets close enough to a gameobject called beacon. I’m storing all the beacons in an allBeacons array. The functionality is executing within Game mode in the editor, but it doesn’t seem to be executing within the build? Initially I thought my beacon gameobject was somehow inactive while the game is running in the build, but from what I can understand it should be active once it is instantiated?
My beacon gameobject gets created from a prefab. I don’t see the nullreferenceexception within the console when I run it in the editor. I read up on the coroutine actually being an object and being set on a change in state, but I’m not sure how to structure my code such that this is true. I currently have the detection set within the beacon class, but I am considering switching it to a different monobehavior considering the beacon class is part of a prefab. In any case, I don’t know best practice.
NullReferenceException: Object reference not set to an instance of an object
at Beacon+<ProximityDetection>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0
UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Beacon:Update()
void Update ()
{
if (allBeacons.Length > 0) {
for (int i = 0; i < allBeacons.Length; i++) {
if(allBeacons _&& allBeacons*.activeInHierarchy){*_
_ StartCoroutine (ProximityDetection (allBeacons , 3.0f));
* }
}
}
}
IEnumerator ProximityDetection (GameObject b, float waitTime)
{
if (Vector3.Distance (fps.transform.position, b.transform.position) <= detectionRange && b.GetComponent().Equals(null)) {
Light light = b.AddComponent ();
light.GetComponent ().type = LightType.Point;
light.shadows = LightShadows.Hard;
light.GetComponent ().intensity = 2.0f;
light.GetComponent ().color = new Color (UnityEngine.Random.value / 2.0f + 0.5f, UnityEngine.Random.value / 2.0f + 0.5f, UnityEngine.Random.value / 2.0f + 0.5f);
yield return new WaitForSeconds (waitTime);
StartCoroutine (ProximityDetection (b, 3.0f));
} else {
Destroy(b.GetComponent());
}
}*_