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 houses = new List();
List foods = new List();
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
↧