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 09, 2015 at 02:00 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DaiMangouDev · Feb 09, 2015 at 12:07 AM · c#arrayboolean

How do I make ALL other booleans in a bool array false when one is true ?

I am working with boolean arrays and I have been having trouble getting all the booleans in an array to be false when one of them is set to true.

 I have been trying this code but it does not work at all.

 

        private static bool SetUP,SetDown,SetLeft,SetRight;
         private static bool[] boolarray = {SetUP,SetDown,SetLeft,SetRight};
         
             bool SetBoolArrays(bool[] array, int lowVal, int highVal, bool State)
             {
                for (int i = lowVal; i < highVal; i++)  { array[i] = State; } return State;
         
             }
         
         
         void OnGUI()
         {
         // make everything from position 1 to array.Lenth false
      
     //method 1 
     if(boolarray[0])SetBoolArrays(boolarray,1,boolarray.Length,false); // does not work
     
     //method 2 
      if (boolarray[0])
                 {
 // will be set to true but will not be set to false when boolarray[1] is true
                     System.Array.Clear(boolarray, 0, boolarray.Length);
                     SetUp = true;
                 }
 
          if (boolarray[1])
                 {
 // will be set to true but will not be set to false when boolarray[0] is true
                     System.Array.Clear(boolarray, 0, boolarray.Length);
                     SetDown = true;
                 }
     
     
         }




Comment
Add comment · Show 8
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 AngryBurritoCoder · Feb 09, 2015 at 12:35 AM 0
Share

what happens? erors ?

avatar image DaiMangouDev · Feb 09, 2015 at 12:41 AM 0
Share

nothing happens . absolutely nothing . I have just tried System.Array.Clear(boolarray, 0, boolarray.Length); and still , nothing happens

avatar image Bonfire-Boy · Feb 09, 2015 at 01:00 AM 0
Share

That code sets all the values in your array (except the first one) to false, but they've all been initialised to false anyway (because they're initialised to the values of 4 bools that have been declared without initialisation - they take the default value). So it doesn't look like there's any code there to change anything.

The point is, how do know that nothing happens? Have you tried logging the array values before and after the call to SetBoolArrays()?

It's not really clear to me what you're trying to achieve. The question is ambiguous at best (is it "when ANY one of them is true" or "when a particular one is true"). The code suggests maybe you want to do it when the first one is true, in which case you could change your OnGUI to

 if (boolarray[0]) SetBoolArrays(boolarray,1,boolarray.Length,false);
 

Also there's a syntax error in the first line (comma after bool) so I wouldn't expect this code to even compile!

avatar image DaiMangouDev · Feb 09, 2015 at 01:20 AM 0
Share

yeahn I have run a Debug.Log. I just wrote this code here for an example, I will fix the syntax error . I already tried that method , it wont work.

nothing will be set to false in the array but if you Debug.Log(array[i]) in the SetBoolArray function in the for loop , it will print false for each but nothing will actually be set to false .

as a metter of fact anything can be set to true in code , but nothing is set to false .

i just tried

 if (boolarray[0])
         {
             System.Array.Clear(boolarray, 0, boolarray.Length);
             SetUp = true;
         }
  if (boolarray[1])
         {
             System.Array.Clear(boolarray, 0, boolarray.Length);
             SetDown = true;
         }

both will be made true , nothing will be made false. which i don't understand

avatar image Bonfire-Boy · Feb 09, 2015 at 01:39 AM 1
Share

@meat5000 That makes sense if one only ever wants one of them to be true. But (and B$$anonymous$$ayne's answer provides a handy example here) you might only want to clear all the other clothing flags when "WetSuit" is true, not for example when "Hat" is true :) But this just goes to support what your saying, that the best way to do whatever-it-is is going to depend on the application.

Show more comments

2 Replies

  • Sort: 
avatar image
2

Answer by BMayne · Feb 09, 2015 at 12:52 AM

I would suggest taking a look at EnumFlags. It's pretty much what you are trying to achieve but it has the advantage of mixing values and being much smaller on memory(Not that a bool is huge). There are some advanced stuff with the code below but it should be pretty simple to understand when you read over it.

 using UnityEngine;
 using System.Collections;
 
 public class FlagTest : MonoBehaviour 
 {
   /// <summary>
   /// The types of clothing our person can wear. It uses bitwise to
   /// give it the ability to have multiple  states. If everything
   /// were off it would be 000000000 and if everythign were on it would be 111111111. What
   /// we do is turn that ones we want on to 1. So say we wanted a dress with sandles it would
   /// be 0100001000.
   /// </summary>
   [System.Flags]
   private enum ClothingTypes : int
   {
     Hat =        1 << 0,
     Sandles =    1 << 1,
     Toga =       1 << 2,
     PaperBag =   1 << 3,
     Shorts =     1 << 4,
     PantSuit =   1 << 5,
     Dress =      1 << 6,
     Sunglasses = 1 << 7,
     Earings =    1 << 8,
     Socks =      1 << 9 // Bitshifting :) 
   }
 
 
 
   /// <summary>
   /// The clothing they are wearing now. 
   /// </summary>
   private ClothingTypes currentlyWearing; 
 
   public void Awake()
   {
     //Are we wearing a hat?
     Debug.Log( "Wearing Hat: " + HasFlag( ClothingTypes.Hat ) );
 
     //Well now we are
     SetFlag( ClothingTypes.Hat, true );
 
     //Lets just make sure we are wearing our hat
     Debug.Log( "Wearing Hat: " + HasFlag( ClothingTypes.Hat ) );
 
     //Now take the hat off
     SetFlag( ClothingTypes.Hat, false );
 
     //This just confirms that in fact we don't have a hat on anymore.
     Debug.Log( "Wearing Hat: " + HasFlag( ClothingTypes.Hat ) );
 
     //Now put on that dress
     SetFlag( ClothingTypes.Dress, true );
 
     //Are we wearing a dress with our sandles?
     Debug.Log("Wearing PantSuit & Sandles: " + HasFlag(ClothingTypes.Sandles | ClothingTypes.PantSuit));
 
     //Turns out we only had a dress on. Lets put on dem sandles
     SetFlag( ClothingTypes.Sandles, true );
 
     //We can now confirm that we are wearing a dress with our sandles
     Debug.Log( "Wearing PantSuit & Sandles: " + HasFlag( ClothingTypes.Sandles | ClothingTypes.PantSuit ) );
 
     //????
 
     //Profit!
   }
 
   /// <summary>
   /// Is this flag currently on?
   /// </summary>
   /// <param name="flags">What is the name of the flag?</param>
   /// <returns>If it's on our not</returns>
   private bool HasFlag( ClothingTypes flags )
   {
     return (currentlyWearing & flags) != 0;
   }
 
   /// <summary>
   /// Set the value of the flag. It can be anything you want if all you want is true of false.
   /// </summary>
   /// <param name="flags">The flag you want to set</param>
   /// <param name="val">If you want it true of false</param>
   private void SetFlag( ClothingTypes flags, bool val )
 {
     if(val == true)
     {
     currentlyWearing |= flags;
     }
   else
   {
     currentlyWearing &= ~flags;
   }
 }
 }

You can then turn everything off by going

  currentlyWearing = 0


Edit: As Bonfire Boy said I did not add the answer to your question. To turn all other bools off you could do

 currentlyWearing = ClothingTypes.Hat;
 
 // Or event all off but two
 currentlyWearing = ClothingTypes.Hat | ClothingTypes.Toga
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 Bonfire-Boy · Feb 09, 2015 at 01:12 AM 0
Share

I think you're right that this is probably the kind of thing they're after, but it doesn't yet address the question of setting all the other flags to false if one of them is true. I guess that would be something like

 if (HasFlag(ClothingTypes.Hat))
 {
    currentlyWearing = ClothingTypes.Hat;
 }
avatar image BMayne · Feb 09, 2015 at 01:24 AM 0
Share

You are correct!

avatar image DaiMangouDev · Feb 09, 2015 at 01:38 AM 0
Share

hey, thanks for all your help. thanks to Bonfire Boy I think I can fix the issue now.

avatar image
1

Answer by Bonfire-Boy · Feb 09, 2015 at 01:25 AM

Still not quite clear to me, but one thing I think you're not understanding is that when you do this

private static bool[] boolarray = {SetUP,SetDown,SetLeft,SetRight};

it's creating an array containing 4 new bools which are initialised to the same values as those 4 variables. They're not references to them.

So changing the values of SetDown, SetUp, etc has no impact on your array.

I think BMayne's answer is pointing you in a good direction.

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 09, 2015 at 01:53 AM 0
Share

thank you both, I have gotten this fixed. @Bonfire Boy as you had made mention to , my booleans in boolarray were not referencing to the initially declared booleans.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to make the Boolean equal to true for only one object 1 Answer

bool[string] = true; array possible? 2 Answers

Array Of Bools 4 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