How can I check values of all array/List quickly

I have a List<> of gameobjects, each gameobject in the List has an accesible “error” member assigned to it during simulation.

I’d like to trigger a (one) global bool value to true, when the value of “error” in gameobjects in the List<> is between -0.05 and 0.05.

I’d do this, setup an array of int, matching the number of entries in List<>, check the entry of List<> one by one, if condition met, assign 1. If the total of 1s in the int == entry in List<> than switch a global bool to true.

My approach is rather long and winding. My question is, is there a shorterway?

You can use LINQ methods (Any or All), e.g.

bool allErrorsAreInRange = !gameObjects.Select(x => x.GetComponent<YourComponentWithErrorInfo>().error)
                        .Any(e => e < -0.05f || e > 0.05f);

You need using System.Linq for above code to work.

The drawback of this code is that GetComponent might be called multiple times, up to the number of elements in your list. But in your current code you have the same problem (well - not exactly, since you do your check for each element, while above LINQ code does it until it finds invalid value). If you want to use it often, I would consider creating a list of YourComponentWithErrorInfo instead.