- Home /
after foreach . my Update() doesn't continue
I have foreach in Update() and after the foreach loop it moves to run other scripts updates and when it comes back it starts the update again.
top of my Update():
void Update()
{
GameObject[] nearby = GetNearbyObjects(SeeDistance);
GameObject nearestHouse;
GameObject nearestFood;
List<GameObject> houses = new List<GameObject>();
List<GameObject> foods = new List<GameObject>();
foreach (var obj in nearby)
{
if (obj.CompareTag("House")) { houses.Add(obj); }
else if (obj.CompareTag("Eatable")) { foods.Add(obj); }
}
nearestHouse = getNearest(houses);
nearestFood = getNearest(foods);
I used debugging mode to see what it runs and when and it didn't run anything more in that script's Update after the foreach loop before it starts over.
And unity gives me an error: NullReferenceException: Object reference not set to an instance of an object HumanBrain.Update () (at Assets/HumanBrain.cs:33)
the line that error is referring is: if (obj.CompareTag("House")) { houses.Add(obj); } inside the Foreach loop
Answer by Bunny83 · Aug 17, 2021 at 03:40 PM
Well, your issue is pretty obvious. Your "nearby" list or array contains elements that are either null, or are dead objects. When an exception happens the code can not continue since this is an exceptional state that the code can not recover from. So whenever an exception happens, the next upper exception handler will be called to sort things out. To handle exceptions manually you would use a try-catch block. Whenever an exception is thrown inside the try block, the execution is terminated and continued in the catch block. If there is no try-catch block, the exception will bubble up all the way to Unity. Unity will actually catch the exception at the point where it called your Update method. This is the point where Unity will generate the log message and continue with business on the next object.
This is the most fundamental knowledge how computers work and doesn't have much to do with Unity. In a "normal" application when an exception is not catched at all, the exception will bubble up to the operating system which will terminate your whole application because the OS has no idea how or where it should continue your application. If such an error happens in the OS / kernel code the whole OS can not continue. In this case you get a kernel panic, (aka blue screen on windows). Luckily the OS kernel nowadays is seperated and protected enough from normal applications that this is almost impossible.
All that said, in order to "fix" your problem you want to either make sure your "nearby" collection does not contain invalid elements, or if you can have null values or dead objects in that collection by design, you should check if the value is null. The easiest solution is a guard clause like this:
foreach (var obj in nearby)
{
if (obj == null)
continue;
if (obj.CompareTag("House")) // [ .... ]
So if the current element we are processing is null, we simply skip the rest of the for loop body and just "continue" with the next element.