how do i check if all booleans in an array are false? C#
Hi guys, i have a bunch of scripts on my enemys that have a PlayerVisable Boolean. I've managed to get all these booleans into an array on my player script, and basically all i want to do is check if any enemy's see me, and if they do, set my players Alert Boolean to true. it seems really simple but i cant seem to figure out just how it works. thanks so much for your help
using UnityEngine;
using System.Collections;
using System.Linq;
public class Player_manager : MonoBehaviour {
public Transform PlayerCam;
public float Speed;
Rigidbody RB;
public bool Alert;
public GameObject[] enemys;
public Enemy_FOV[] enemyFOV;
public bool[] enemyAlert;
// Use this for initialization
void Start () {
RB = gameObject.GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
enemys = GameObject.FindGameObjectsWithTag ("Enemy");
enemyFOV = new Enemy_FOV[enemys.Length];
for(int i = 0; i < enemys.Length; i++){
enemyFOV[i] =enemys[i].GetComponent<Enemy_FOV> ();
}
enemyAlert = new bool[enemyFOV.Length];
for(int i = 0; i < enemyFOV.Length; i++){
enemyAlert[i] =enemyFOV[i].playerVisable;
}
if (Input.GetButtonUp ("Fire1")) {
Jump_Start ();
}
}
so basically i just want to make Alert true, when any of the enemyAlert array bools are true.
Answer by Light997 · Feb 06, 2016 at 12:22 PM
You can loop through all the elements in the array and return once you've found one that's true.
Here:
bool isAlert(bool[] enemyAlert)
{
for (int i = 0; i < enemyAlert.Length; i++)
{
if (enemyAlert[i])
{
return enemyAlert[i];
}
}
return false;
}
This will loop through the bools and return true if one of them is true.
You can call the function like this:
Alert = isAlert(enemyAlert);
thanks so much for your answer, can you elaborate a tiny bit more on how to integrate that with my code. putting that in my update function doesn't seem to work. the line 'bool isAlert(bool[] enemyAlert)' seems to come up with some errors. thank you so much!
edit: never $$anonymous$$d! i figured it out. thank you so much!
@Pendragon420 Okay...
(Tip: if you type @(username) the person is informed of your comment, I just happened to come by again)
So, you put this function in your code outside of any other functions, just inside the class. (I mean the bool isAlert(enemyAlert){...}).
Then, whenever you want to check whether you are in the Alert state, you call the function with the lower code, you can do this anywhere within the class. That includes Update, Start, etc.
If you need more help, inform me with @Light.
You can also mark an answer as correct if it is correct, hit the checkmark next to it. The answer should then be highlighted in green. This helps others to quickly find the best answer.
Answer by LittleRainGames · Dec 21, 2017 at 02:12 AM
You can also do
bool isAlert = enemyAlert.Any(x => x);
Your answer
Follow this Question
Related Questions
Bool[] Array index is out of range 0 Answers
Raycast not changing value 0 Answers
check bool across a script 1 Answer
I'd like my object to be active only when space key is NOT pressed 1 Answer
Applying Material to individual clones 0 Answers