- Home /
Experiencing NullReferenceException only in build with Coroutine
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[i] && allBeacons[i].activeInHierarchy){
StartCoroutine (ProximityDetection (allBeacons [i], 3.0f));
}
}
}
}
IEnumerator ProximityDetection (GameObject b, float waitTime)
{
if (Vector3.Distance (fps.transform.position, b.transform.position) <= detectionRange && b.GetComponent<Light>().Equals(null)) {
Light light = b.AddComponent<Light> ();
light.GetComponent<Light> ().type = LightType.Point;
light.shadows = LightShadows.Hard;
light.GetComponent<Light> ().intensity = 2.0f;
light.GetComponent<Light> ().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<Light>());
}
}
Answer by Cherno · Jun 24, 2015 at 10:23 PM
The easiest way to avoid this is to check if the GameObject variable's value is null before trying to access it.
Either do it before starting the Coroutine:
if(allBeacons [i] != null) {
StartCoroutine (ProximityDetection (allBeacons [i], 3.0f));
}
or inside to CoRoutine itself, since you CAN pass it a GameObject variable whose value is null, but once you actually try to access it, you will get the NullReference error.
IEnumerator ProximityDetection (GameObject b, float waitTime)
{
if(b == null) {
break;
}
if (Vector3.Distance (fps.transform.position, b.transform.position) <= detectionRange && b.GetComponent<Light>().Equals(null)) {
//... and so on
so I actually tried that yesterday and printed a debug statement if it was null, but it wasn't so I think I'm probably using the yield return incorrectly. Note, like I said this works in game mode, but not build mode so I am looking for an explanation why.
Your answer
Follow this Question
Related Questions
Accessing a prefab after instantiating results as null 1 Answer
NullReferenceException 1 Answer
How to start a Coroutine in another Script? 2 Answers
i am not able to control each instance of an object in update function.help. 0 Answers
NullReferenceException MaskableGraphic.UpdateCull() upon Loading Player Data. 0 Answers