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 weltraumaffe · May 10, 2013 at 07:06 PM · c#unity 4

Enum Flags not working (selecting wrong value)

Hi

i want to use a System.Flags enum in a component. Also i wrote a custom editor.

This is the Enum.

 [System.Flags]
 public enum ConnectorGender {
     Neutral = 1,
     Female = 2,
     Male = 4
 }

The custom editor reads and writes the value with the following line of code

 instance.compatibleGender = (ConnectorGender)EditorGUILayout.EnumMaskField("Genders", instance.compatibleGender);

However when I select a combination of values they are always wrong and when i select everything the value gets -1?

How can i fix this?

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
Best Answer

Answer by rutter · May 10, 2013 at 08:37 PM

Remember that flag enums are internally represented as a series of bits:

 public enum ConnectorGender {
     Neutral = 1, //bits: 0000 0001
     Female = 2,  //bits: 0000 0010
     Male = 4     //bits: 0000 0100
 }

If you set both Neutral and Male, you'll see a bit pattern like this:

 0000 0101 //Neutral, Male

When Unity sets "nothing", it sets all bits to 0.

 0000 0000 //Nothing (integer value: 0)

When Unity sets "everything", it sets all bits to 1.

 1111 1111 //Everything (integer value: -1)

When you then uncheck a value, it will unset the corresponding bit.

 1111 1101 //Everything, minus Female (integer value: -3)
 0000 0101 //Same significant bits (integer value: 5)

These seem like totally different values, but remember that the system only really cares about the specific bits it's working with. You unset the bit for "Female", but other bits are unchanged.

If you try a custom editor that works with Unity's own StaticEditorFlags enum, you'll see this holds true even for their own enums.

Is this a problem? Depends on how you use those values. If all you ever do is check against individual bits, you'll be fine. If you're expecting that a particular set of flags will always produce the same value, you might not be.

You can force Unity to unset the extra bits by iterating through them right after the EditorGUILayout call, something like this:

 if (instance.compatibleGender < 0) {
     int bits = 0;
     foreach (var enumValue in System.Enum.GetValues(typeof(ConnectorGender))) {
         int checkBit = (int)instance.compatibleGender & (int)enumValue;
         if (checkBit != 0) {
             bits |= (int)enumValue;
         }
     }
     instance.compatibleGender = (ConnectorGender)bits;
 }
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 weltraumaffe · May 10, 2013 at 10:46 PM 0
Share

Thanks for this awesome explanation!

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

14 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Mapping from External to Unity World Coordinates for eye tracking 0 Answers

Rotate Camera 1 Answer

TextMeshPro issues 2 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