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 /
avatar image
0
Question by Xeong-Hu · Mar 05, 2015 at 03:22 PM · script.

How exactly can I group booleans as one?

Well here's what I mean.

I have these Booleans that list as true or false when something is activated and what not.

But what I would like to do is this.

Say I have like over 20 Booleans and I'd like to check them all off as False when ever a certain event happens. and I'd like to check them all off as true when some other certain event happens. Sounds pretty simple right?

I just don't know exactly if it's possible or how to do this.

Hmm, Help would be much appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tigertrussell · Mar 05, 2015 at 03:32 PM

There are many ways that you could accomplish what you are trying to do.

You could just use an array:

 bool myBools[] = { false, false, false, false };
 
 void SetAll(bool b) {
    for(int i=0; i<myBools.Length; i++) {
       myBools[i] = b;
    }
 }

Or you could use a bitmask (note: I have never done this in C# or with Unity before). Check this out, though.

Edited to explain maps (in C# they are called Dictionary but in computer science in general, they are maps):

So in the above example we created an array of bools myBools but now we want to be able to give each one a specific name and reference it by that name.

For example, let's say we're creating a basic inventory system of fruit (see comments below). In this super-simple example, we'll just want to determine whether we even have any of a specific fruit.

We begin by creating a Dictionary which will map from a string to a bool because we want to be able to determine if we have a fruit knowing only its name.

Below is some example code showing how you could create a basic inventory system:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ExampleClass {
     //Declare our basic inventory
     Dictionary<string, bool> fruitInventory = new Dictionary<string, bool>();
     
     //A more advanced "how many do we have" inventory would be defined like this
     Dictionary<string, int> fruitQuantity;
     
     //We could also make a class for more advanced data
     public class FruitInventoryData {
         int numRotten;
         int numFresh;
         bool canMakeWine;
     }
     
     //And use a dictionary like this
     Dictionary<string, FruitInventoryData> fruitAdvancedData;
     
     public void ExecuteExample() {
         //Add some fruit to our basic inventory
         fruitInventory.Add("apples", true);
         fruitInventory.Add("bananas", false);
         
         //Can also access it like an array, but ONLY to change it.
         
         //This causes an error, because we have no data on mangoes yet
         //fruitInventory["mangoes"] = false;
         
         //But this does not, because we DO have data about bananas
         fruitInventory["bananas"] = true;
         
         //We can be safe like this - it doesn't get executed because we check if we have the key, first
         if(fruitInventory.ContainsKey("mangoes")) {
           fruitInventory["mangoes"] = false;
         } else {
           fruitInventory.Add("mangoes", false);
         }
         
         //And we can also easily iterate over dictionaries
         foreach(KeyValuePair<string, bool> fruitData in fruitInventory) {
             string fruit = fruitData.Key;
             bool haveThisFruit = fruitInventory[fruit];
             if(haveThisFruit) {
                 Console.Write("We have " + fruit + "\n");
             } else {
                 Console.Write("We DO NOT have " + fruit + "\n");
             }
         }    
     }

If you ran ExecuteExample you would get this output:

 We have apples                                                                                                                                                                                                                     
 We have bananas                                                                                                                                                                                                                    
 We DO NOT have mangoes   

And here is a link to this executing.

Comment
Add comment · Show 12 · 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 tigertrussell · Mar 05, 2015 at 04:05 PM 0
Share

Actually, probably shy away from the bitmask unless you take your time and read up on them...

avatar image meat5000 ♦ · Mar 05, 2015 at 04:08 PM 0
Share

Bit$$anonymous$$asks would be awesome if bools still supported 0 and 1 like t' good old days.

avatar image tigertrussell · Mar 07, 2015 at 07:56 PM 1
Share

You're looking for a map. I'll edit my answer. You should grab a book on data structures, though (or read the Wikipedia page).

avatar image tigertrussell · Mar 07, 2015 at 08:32 PM 1
Share

Okay - there it is.

avatar image tigertrussell · Mar 07, 2015 at 08:34 PM 1
Share

I don't know why I can't comment anymore. This website is always so broken. But I updated the answer; hopefully you get a notification.

Show more comments

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

21 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

Related Questions

calling enum State to change correctly but it is not changing 1 Answer

Using conditional logic when updating a text field (efficient code question) 1 Answer

Brackeys EnemyAi Script 0 Answers

how i increment a variable 0 Answers

Shooting projectiles to the position of the mouse click? 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