Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Pendragon420 · Feb 06, 2016 at 11:36 AM · c#arraybooleancheckall

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
6

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);
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pendragon420 · Feb 06, 2016 at 01:42 PM 0
Share

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!

avatar image Light997 Pendragon420 · Feb 06, 2016 at 06:04 PM 0
Share

@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.

avatar image Light997 Pendragon420 · Feb 06, 2016 at 06:05 PM 0
Share

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.

avatar image
0

Answer by LittleRainGames · Dec 21, 2017 at 02:12 AM

You can also do

 bool isAlert = enemyAlert.Any(x => x);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

85 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges