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 Intervention · Jan 02, 2015 at 11:10 AM · listlayermaskcompare

List has strange values at run time

I'm attempting to make a public List placeableLayers in my script where I can choose the layers I want to be in the list via the inspector.

I set up my list in the inspector like normal (I can choose from the list of available layermasks and put them in the list), but when the code runs and compares the LayerMask's 'value' field against a gameObject.layer item, the LayerMask's value (from the List) has bizarre out of bounds layermask values, like 2048 and 4098 (powers of 2, interestingly enough).

Am I misunderstanding how to compare layer masks?

The code that does the comparison:

 // placeableLayerMasks is a List<LayerMask> declared public for this class, filled in via the inspector with valid LayerMasks.
 // is this object configured with a layer that matches the layer we 'hit' with our raycast down?
 private bool IsLayerPlacable(int layer) // 'int layer' is the result of gameObject.layer
  foreach (LayerMask placeableLayer in placeableLayerMasks)
         {
             Debug.Log("Comparing " + placeableLayer.value + " with " + layer);
             if (layer == placeableLayer.value) // TODO these are broken playableLayer.value is garbage??
             {
                 Debug.Log("TRUEEEE");
                 return true;
             }
         }

placeableLayer.value is not a valid layer value, even though I've configured these via the inspector (and when running, viewing this list in the inspector still shows the appropriate values).

Comment
Add comment · Show 3
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 Krizzen · Jan 02, 2015 at 01:06 PM 0
Share

You need to provide some details. Lists should have NO data at runtime.

avatar image Landern · Jan 02, 2015 at 01:13 PM 0
Share

Lists should have NO data at runtime.

That would make them worthless wouldn't it?

I mean if you did

 List<int> nums = new List<int>() { 1, 2, 3, 4, 5 };

That would ensure the list of int called nums would have data. Whatcha mean?

avatar image Intervention · Jan 03, 2015 at 12:11 AM 0
Share

Also, just to clarify - I have initialized my lists with data via the inspector in Unity. They do in fact have data when the game runs, but as pointed out below, I was wrong about what type of data.

1 Reply

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

Answer by steakpinball · Jan 02, 2015 at 03:00 PM

A single LayerMask object can store several layers to match. LayerMask.value is a bit mask representing all layers. The right most bit represents layer 0. The leftmost bit represents layer 32. A bit of 1 means to match that layer, while 0 means to not match. A value of 1 ( 00000000000000000000000000000001) means layer 0 (Default) was selected. A value of 2048 ( 00000000000000000000100000000000) means layer 11 was selected. This is useful because you can specify all layers in a single LayerMask object. If layers 0 and 11 are selected the value would be layer 0 (1) + layer 11 (2048) resulting in 2049 ( 00000000000000000000100000000001). To check whether a layer should be matched use bitwise and with a left shifted 1. For example, to check whether layer 11 is matched by the layer mask do layerMask.value & 1 << 11 == 1 << 11.


 private bool IsLayerPlacable(int layer) { // 'int layer' is the result of gameObject.layer
   foreach (LayerMask placeableLayer in placeableLayerMasks) {
     var bitMask = 1 << layer;
     if (placeableLayer.value & bitMask == bitMask) {
       return 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 Intervention · Jan 02, 2015 at 09:08 PM 0
Share

Ahh yes, this solved my problem.

Thanks for pointing this out... Indeed I was making a silly mistake - forgot that what I was really comparing against was the entire bitmask (and of course this explains my power of 2 value).

Cheers.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

javascript, default object comparator 1 Answer

bool to true from a list comparison. 1 Answer

Comparing two lists to find the difference 1 Answer

Find out if two children/gameObjects have the same parent. 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