Search Unity

Check if any array item is true

Discussion in 'Scripting' started by Studio_Akiba, Aug 29, 2015.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    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
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Barrier : MonoBehaviour
    4. {
    5.  
    6.     public enum State
    7.     {
    8.         Active,
    9.         Inactive,
    10.     }
    11.  
    12.     public State state;
    13.     public WeightedButton[] buttons;
    14.  
    15.     void Update()
    16.     {
    17.         if (state == State.Active)
    18.         {
    19.             MeshRenderer meshObject = GetComponent<MeshRenderer>();
    20.             meshObject.enabled = true;
    21.             BoxCollider meshCollider = GetComponent<BoxCollider>();
    22.             meshCollider.enabled = true;
    23.         }
    24.         else
    25.         {
    26.             MeshRenderer meshObject = GetComponent<MeshRenderer>();
    27.             meshObject.enabled = false;
    28.             BoxCollider meshCollider = GetComponent<BoxCollider>();
    29.             meshCollider.enabled = false;
    30.         }
    31.  
    32.     }
    33.  
    34.     public void FixedUpdate()
    35.     {
    36.         foreach (WeightedButton wb in buttons)
    37.         {
    38.             if (wb.isEnabled)
    39.             {
    40.                 state = State.Inactive;
    41.             }
    42.             if (!wb.isEnabled)
    43.             {
    44.                 state = State.Active;
    45.             }
    46.         }
    47.     }
    48. }
    WeightedButton.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WeightedButton : MonoBehaviour
    5. {
    6.     public bool isEnabled;
    7.     public GameObject buttonObject;
    8.     public Vector3 buttonOrigin = new Vector3(0f, 0.1f, 0f);
    9.     public Vector3 buttonTarget = new Vector3(0f, 0.05f, 0f);
    10.     public float moveSpeed;
    11.  
    12.     void OnCollisionEnter(Collision other)
    13.     {
    14.         StartCoroutine(MoveDown(buttonTarget));
    15.  
    16.         if (other.gameObject.tag == "Box" || other.gameObject.tag == "Player")
    17.         {
    18.             isEnabled = true;
    19.         }
    20.     }
    21.  
    22.     void OnCollisionExit(Collision other)
    23.     {
    24.         StartCoroutine(MoveUp(buttonOrigin));
    25.  
    26.         if (other.gameObject.tag == "Box" || other.gameObject.tag == "Player")
    27.         {
    28.             isEnabled = false;
    29.         }
    30.     }
    31.  
    32.     IEnumerator MoveDown(Vector3 target)
    33.     {
    34.         while (buttonObject.transform.localPosition != target)
    35.         {
    36.             buttonObject.transform.localPosition = Vector3.MoveTowards(buttonObject.transform.localPosition, target, moveSpeed * Time.deltaTime);
    37.             yield return 0;
    38.         }
    39.     }
    40.  
    41.     IEnumerator MoveUp(Vector3 origin)
    42.     {
    43.         while (buttonObject.transform.localPosition != origin)
    44.         {
    45.             buttonObject.transform.localPosition = Vector3.MoveTowards(buttonObject.transform.localPosition, origin, moveSpeed * Time.deltaTime);
    46.             yield return 0;
    47.         }
    48.     }
    49. }
     
  2. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    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
    Code (CSharp):
    1. bool AnyButtonEnabled()
    2. {
    3.     foreach(var button in buttons)
    4.         if (button.isEnabled)
    5.             return true;
    6.          
    7.     return false;
    8. }
    So, I guess, your use case:
    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3.     state = AnyButtonEnabled() ? State.Active : State.Inactive;
    4. }
     
    Kiwasi likes this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  5. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Code (CSharp):
    1. buttons.Any(b => b.isEnabled)
     
    ThorvaldM likes this.