Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Feb 08, 2015 at 09:25 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DaiMangouDev · Feb 08, 2015 at 02:23 PM · c#array

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.

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

3 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by Mmmpies · Feb 08, 2015 at 02:42 PM

 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[i] == false)
                 return false;
         }
         return true;
     }
 }

This should work, just be careful with passing only up to 6 for an array of 7.

Comment
Add comment · Show 16 · 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 DaiMangouDev · Feb 08, 2015 at 03:05 PM 1
Share

okay great! , this is good . thanks for the help and the code .

avatar image Mmmpies · Feb 08, 2015 at 03:14 PM 0
Share

And thumbs up @ruchmair for not only ticking an answer but thumbs up on the other 2 solutions offered :¬)

avatar image daneislazy · Feb 08, 2015 at 05:38 PM 1
Share

yea, you would have to write another function for that, it wouldn't need a return type but would need another parameter for the bool so you would call something like SetBoolArray(array, a, b, boolName); and just use the same for loop but ins$$anonymous$$d of the If & returns just put in boolarray[I] = boolName;

avatar image Mmmpies · Feb 08, 2015 at 07:39 PM 1
Share
     bool CheckBoolArrays(string myArray, int lowVal, int highVal)
      {
         for(int i = lowVal; i <= highVal; i++)
         {
 
             switch(myArray)
             {
                 case "boolarray" :
                     if(boolarray[i] == false) 
                     {
                         return false;
                     }
                     break;
                 case "otherarray" :
                     if(otherarray[i] == false)
                     {
                         return false;
                     }
                     break;
             }
         
         }
         return true;
     }

Bit of a cheesy way to do it but if you're having issues it'll get you through. Incidentally are these all being called from the same script, or will the bools be from all different scripts?

If from different then you'll need to reference the script to get to the bool. Call the above with:

 returnedBool = CheckBoolArrays("boolarray", 1, 4);
 Debug.Log (returnedBool);
 
 returnedBool = CheckBoolArrays("boolarray", 4, 6);
 Debug.Log (returnedBool);
avatar image daneislazy · Feb 10, 2015 at 01:22 AM 1
Share

ah sorry if I was unclear, it's crashing because you are endlessly recurring the same function.

But to fix it just change the return to:

 return array;

and when you call it use something like:

 originalArray = SetBoolArrays(originalArray, 3, 7, false);

that should return the array that is manipulated by the method to the original array.

Show more comments
avatar image
2

Answer by Local-Minimum-2 · Feb 08, 2015 at 02:59 PM

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");
Comment
Add comment · Show 1 · 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 DaiMangouDev · Feb 08, 2015 at 03:09 PM 0
Share

also great, this is even more concise . thanks for the help and the code

avatar image
2

Answer by AlwaysSunny · Feb 08, 2015 at 02:44 PM

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[i]) 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[i]) result = false;
   // now "result" answers your if-range-true query
Comment
Add comment · Show 2 · 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 daneislazy · Feb 08, 2015 at 03:03 PM 0
Share

Yea I was thinking extension too. In order to do that make sure it is in a separate class that is public static, and that does not inherit from monodevelop and use this bool[] source in the method parameters.

Sample:

 public static class Extensions {
     public static Vector2 ToVector2(this Vector3 v3) {
         return new Vector2(v3.x, v3.y);
     }
 }

I think there is also a Unity tutorial on it. And definitely check for index out of range.

avatar image DaiMangouDev · Feb 08, 2015 at 03:06 PM 0
Share

sure , I will be using that snippet in a separate class . things will be more efficient that way . thanks for all the help.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Loop through multidimensional array in C#(Unity) 1 Answer

Unity event calling function gets nullreferencexception on bool 0 Answers

Change sprite of image 1 Answer


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