Check if any array item is true

I have a script referencing a script called WeightedButton, which has a public boolean called isEnabled.

What I need to do is check to see if ANY of the items in said array have an isEnabled value of true, and to run some code.

Everything I have found online so far points to checking if all are true, I need to find out if any of them are true.

The current code for the main script is laid out like so:
Barrier.cs

using UnityEngine;

public class Barrier : MonoBehaviour
{

    public enum State
    {
        Active,
        Inactive,
    }

    public State state;
    public WeightedButton[] buttons;

    void Update()
    {
        if (state == State.Active)
        {
            MeshRenderer meshObject = GetComponent<MeshRenderer>();
            meshObject.enabled = true;
            BoxCollider meshCollider = GetComponent<BoxCollider>();
            meshCollider.enabled = true;
        }
        else
        {
            MeshRenderer meshObject = GetComponent<MeshRenderer>();
            meshObject.enabled = false;
            BoxCollider meshCollider = GetComponent<BoxCollider>();
            meshCollider.enabled = false;
        }

    }

    public void FixedUpdate()
    {
        foreach (WeightedButton wb in buttons)
        {
            if (wb.isEnabled)
            {
                state = State.Inactive;
            }
            if (!wb.isEnabled)
            {
                state = State.Active;
            }
        }
    }
}

WeightedButton.cs

using UnityEngine;
using System.Collections;

public class WeightedButton : MonoBehaviour
{
    public bool isEnabled;
    public GameObject buttonObject;
    public Vector3 buttonOrigin = new Vector3(0f, 0.1f, 0f);
    public Vector3 buttonTarget = new Vector3(0f, 0.05f, 0f);
    public float moveSpeed;

    void OnCollisionEnter(Collision other)
    {
        StartCoroutine(MoveDown(buttonTarget));

        if (other.gameObject.tag == "Box" || other.gameObject.tag == "Player")
        {
            isEnabled = true;
        }
    }

    void OnCollisionExit(Collision other)
    {
        StartCoroutine(MoveUp(buttonOrigin));

        if (other.gameObject.tag == "Box" || other.gameObject.tag == "Player")
        {
            isEnabled = false;
        }
    }

    IEnumerator MoveDown(Vector3 target)
    {
        while (buttonObject.transform.localPosition != target)
        {
            buttonObject.transform.localPosition = Vector3.MoveTowards(buttonObject.transform.localPosition, target, moveSpeed * Time.deltaTime);
            yield return 0;
        }
    }

    IEnumerator MoveUp(Vector3 origin)
    {
        while (buttonObject.transform.localPosition != origin)
        {
            buttonObject.transform.localPosition = Vector3.MoveTowards(buttonObject.transform.localPosition, origin, moveSpeed * Time.deltaTime);
            yield return 0;
        }
    }
}

Anyone?

I’m not sure what’s causing you confusion, to be honest.

This will return true if any of the buttons in the array are enabled

bool AnyButtonEnabled()
{
    foreach(var button in buttons)
        if (button.isEnabled)
            return true;
          
    return false;
}

So, I guess, your use case:

void FixedUpdate()
{
    state = AnyButtonEnabled() ? State.Active : State.Inactive;
}
1 Like

Iterate through the collection until you find one that is true. If you get to the end of the collection then none of them are true.

buttons.Any(b => b.isEnabled)
1 Like