How to check the state of booleans in an bool array, reading from index "a" to "b"?

Hi I have a boolean array private bool[] boolarray = new bool[7];
lets say i want to check the state of the booleans at positions 3 to 7.

so if positions 3 to 7 == true …

I am hoping to be able to check the state of the boolean in that way or along those lines

how is this done , can it even be done?, if not what is the best method to achieve the desired result.

using UnityEngine;
using System.Collections;

public class CheckArr : MonoBehaviour {

	private bool[] boolarray = new bool[7];
	private bool returnedBool;

	// Use this for initialization
	void Start () {
	
		boolarray[0] = true;
		boolarray[1] = true;
		boolarray[2] = false;
		boolarray[3] = true;
		boolarray[4] = true;
		boolarray[5] = true;
		boolarray[6] = true;

		returnedBool = CheckBoolArrays(1, 4);
		Debug.Log (returnedBool);

		returnedBool = CheckBoolArrays(4, 6);
		Debug.Log (returnedBool);
	}


	bool CheckBoolArrays(int lowVal, int highVal)
 	{
		for(int i = lowVal; i <= highVal; i++)
		{
			if(boolarray *== false)*
  •  		return false;*
    
  •  }*
    
  •  return true;*
    
  • }*
    }
    This should work, just be careful with passing only up to 6 for an array of 7.

This would dutifully print out both debug messages, which I think is the logic you wanted.

using system.linq

bool[] boolarray = new bool[7] {true, false, true, true, true, true, true};
    if (boolarray.Skip(2).Take(5).All(v => v))
        Debug.Log ("All true");
boolarray = new bool[7] {true, false, true, true, false, true, true};
    if (!boolarray.Skip(2).Take(5).All(v => v))
        Debug.Log ("All not true");

If you need the snippet in more than one class, or ever want to do it again in another project, it’s a great idea to start keeping a file of handy static and extension methods.

public static bool CheckBoolRange(bool[] source, int a, int b) {
// here you might have checks to prevent out-of-range or null exceptions      
  for (int i=a; i<=b; i++) if(!source*) return false;*

return true;
}
If that’s more trouble than you want, the loop is a good enough answer:
bool result = true;
for (int i=a; i<=b; i++) if(!source*) result = false;*
// now “result” answers your if-range-true query