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
1
Question by Bicko · Mar 01, 2012 at 02:55 PM · arraybooleannameelement

Array of named booleans

If I create a boolean array:

 public bool[] boolArray;

Then the inspector shows me boolean tick-boxes named "Element 0, Element 1" etc. which isn't really helpful.. how can I change it so that each element in the array has an actual name?

Comment
Add comment · Show 5
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 Jacek_Wesolowski · Mar 01, 2012 at 03:15 PM 0
Share

There is no grammar for that in C#. Array elements share a common name (in this case: boolArray) and are differentiated from each other by their index. You can write boolArray[3] in your code, but you can't write boolArray[name] or boolArray.name.

(well, actually, boolArray[name] could work if you used enums - but it still wouldn't show up in the editor)

If each element in the array actually means something specific, you might want to create a class of named bools, such as:

public class $$anonymous$$yBools { public bool isCorrect; public bool isImportant; public bool isUrgent; public bool isGiraffe; // etc. } public $$anonymous$$yBools boolArray;

Or, you could create an array of simple objects:

public class NamedBool { public string Name; public bool Value; } public NamedBool[] boolArray;

Note that this creates serious overhead, because a string can easily be many times bigger in terms of memory usage than a bool.

There is also a library class in C# called Dictionary, and it does a similar thing, but it's most likely a bit of an overkill in this case.

I guess you could also try some advanced custom editor magic and create custom editor interface that provides the na$$anonymous$$g functionality. It would probably use a data structure like the second example, only you wouldn't need to keep names in memory at runtime.

avatar image Caiuse · Mar 01, 2012 at 03:17 PM 0
Share

Well seeing as both me and @Jacek_Wesolowski have suggested the NamedBool class object option, I would recommend that method above any other.

avatar image CHPedersen · Mar 01, 2012 at 03:24 PM 0
Share

I wouldn't. :) A $$anonymous$$or coupling of data like that shouldn't be a class, it should be a struct. But that's not the main reason. I think this should be a Dictionary because it makes lookups more elegant when you want to access the data later. It's prettier and more succint to access an array with a string and get a boolean, than having to loop through a collection of NamedBoolean classes/structs and return when you find one that matches your search string.

avatar image Jacek_Wesolowski · Mar 01, 2012 at 03:33 PM 0
Share

This should be a struct, but structs didn't show up in the inspector last time I checked.

The rest depends on the usage pattern. From what I gather, the names are needed for user's convenience and won't be needed at runtime. There is no faster way to access an array element than its index. Dictionary saves time if you plan on searching for a small number of values in a large set.

avatar image Bicko · Mar 02, 2012 at 09:53 AM 0
Share

What I'm essentially playing around with is a card game. I actually tried making a class, unfortunately I couldn't find a way to loop through each variable in the class to check which ones are enabled. Here's what I tried (and failed) to use:

 [Serializable]
 public class CardTypes : IEnumerable
 {
     public bool _monster = false;
     public bool _equipment = false;
     public bool _land = false;
     public bool _spell = false;
 }

I think I'm probably going to try the Dictionary that @Christian H Pedersen suggested, but thanks for all the info!

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by CHPedersen · Mar 01, 2012 at 03:07 PM

It sounds like you might need a Dictionary instead. A Dictionary is a datastructure that allows you to link a key to the values that go into the structure. A typical use case is a phonebook, where you link the name of a person (the key is then a string) to his/her phone-number (the value is then an integer).

Try something like this:

     Dictionary<string, Boolean> NamedBooleans = new Dictionary<string, bool>();

     NamedBooleans.Add("This is the name of the first boolean", false);
     NamedBooleans.Add("This is the name of the second boolean", true);

Then, later on, you can look up the booleans like this, for example:

     if (NamedBooleans["This is the name of the first boolean"])
     {
         // Yada yada
     }
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
avatar image
0

Answer by Caiuse · Mar 01, 2012 at 03:04 PM

As far as I am aware you can't assign a name to a boolean, there may be a way of serializing the boolean with a name but I'm not sure.

1) use a Hashtable to store the booleans:

 var hashtable = {};
 
 function Start(){
     hashtable["My boolean"] = true;
     hashtable["Another boolean"] = false;
 }

then you can refer to them now like so...

 if(hashtable["My boolean"]){
       //do stuff
 }

2) make class object for a named boolean:

 var array : NamedBoolean[];
 
 class NamedBoolean {
     var name : string;
     var bool : boolean;
 }

with a class object they will show in the inspector as the "name" variable you assign.

refer to them like so:

 if(array[i].bool){  
      //do stuff
 }

3) similar to a hashtable you could use a generic list

 var myList = new List.<boolean>();

then you can refer to them now like so...

 if(myList["My boolean"]){
       //do stuff
 }
 
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

7 People are following this question.

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

Related Questions

Is it possible to change the element name in an Array List? 1 Answer

How do you save GameObject[]? 1 Answer

Adding booleans to array and randomize 0 Answers

Bulletin Array, change element variables 1 Answer

How to delete a element from mesh rendered and mesh collider. 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