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 jedd.ahyoung · Jul 31, 2014 at 04:40 AM · layermask

Best way to set a GameObject layer programmatically?

In my game I have an ArmamentController, a Monobehaviour that attaches to Actors (players or enemies). This controller has a public LayerMask variable so that I can set the layer for the projectiles (for the collision matrix; friendly bullets don't collide with the player, enemy bullets don't collide with enemies). The really nice thing about this is that in the editor, I get the list of layers (as opposed to stringly typing, which sucks).

This, unfortunately, fails horribly with GameObject.layer, as the LayerMask is an object while GameObject.layer is an int. LayerMask provides the value getter, but that actually returns the bitmask, not the integer of the layer. There doesn't seem to be a function that does this (and I'm not sure why - why not add it as a property to the LayerMask?)

I could get the integer value, but unfortunately the only way to inverse a bitshift function is to use a logarithm, and those are pretty expensive....so if there are a lot of shots, that's something you don't want, right?

Is there another Unity object type besides LayerMask? Layer, perhaps? Or something like that? Or am I forced to use the logarithm method (or some wacky loop or something) or resort to strings? Is there perhaps a built-in enumerator for this, or can I access the Layer list directly...?

Edit: I tried using the ToString() method of my LayerMask variable, hoping that behind the scenes it was using the [Enum] tag somewhere (like this question) but alas, that idea didn't work either.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
19

Answer by Meltdown · Jul 31, 2014 at 05:04 AM

 gameObject.layer = LayerMask.NameToLayer("YourLayerName");
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 jedd.ahyoung · Jul 31, 2014 at 08:36 AM 0
Share

This requires the string name, which I specifically don't have.

avatar image jsalame36 · May 14, 2017 at 12:24 AM 0
Share

I tried this already and it does not work for me. How do you do it in Unity 5?

avatar image antonsem · Jul 27, 2019 at 10:12 AM 0
Share

That is definitely the way to go. Additionally, I would suggest keeping all of the layers in a static class. Like this: https://www.anton.website/quick-tip-for-constants/

avatar image
0

Answer by MakeCodeNow · Jul 31, 2014 at 05:06 AM

Using LayerMask is correct. Just use the bitshift operators . No log required!

Comment
Add comment · Show 2 · 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 gotnull · Jul 27, 2019 at 08:33 AM 0
Share

Care to elaborate on using bitshift operators ins$$anonymous$$d?

avatar image Bunny83 gotnull · Jul 27, 2019 at 10:15 AM 0
Share

Well, it highly depends on what your layermask represents. If it contains more than 2 layers, which one do you actually want? For example layer 8 and layer 11 could be set in the mask. Which one do you want? When you are sure there's only a single layer set in the mask, you can simply scan through the layers and see if it is set or not. Once you found a set one, you know the index.


This could be even optimised into several distinct steps (essentially performs a binary search) to reduce the number of steps. For example

 public static int GetLayerFrom$$anonymous$$ask(Layer$$anonymous$$ask a$$anonymous$$ask)
 {
     uint val = (uint)a$$anonymous$$ask.value;
     if (val  == 0)
         return -1;
     int layer = 0;
     if (val > 0xFFFF) // XXXX XXXX XXXX XXXX 0000 0000 0000 0000
     {
         layer += 16;
         val >>= 16;
     }
     if (val > 0xFF) // XXXX XXXX 0000 0000
     {
         layer += 8;
         val >>= 8;
     }
     if (val > 0xF) // XXXX 0000
     {
         layer += 4;
         val >>= 4;
     }
     if (val > 0x3) // XX00
     {
         layer += 2;
         val >>= 2;
     }
     if (val & 0x2 != 0) // X0
         layer += 1;
     return layer;
 }

Note that this approach would always return the highest layer set in the mask

The more traditional approach would be this

 public static int GetLayerFrom$$anonymous$$ask(Layer$$anonymous$$ask a$$anonymous$$ask)
 {
     uint val = (uint)a$$anonymous$$ask.value;
     if (val  == 0)
         return -1;
     for (int i = 0; i < 32; i++)
     {
         if( (val & (1<<i)) != 0)
         return i;
     }
     return -1;
 }

This approach would always return the smallest layer set in the mask, however the worst case (when looking for layer 31) would require 32 iterations. That's not really something you should be concerned about, even when used several hundreds of times per frame. Though the first approach has a fix runtime no matter which layer and requires 5 steps (for a 32 bit layermask). This has a logarithmic scale. If you had a 64 bit layermask, only a single additional step would be required. Though since Unity uses an int we only have 32 bits.


Note: I've written both functions here from scratch, no syntax checking has been performed and of course they aren't tested. Though you can play through every possible layermask in your head to convince yourself that it works ^^. The XXX in the comments indicate the new "active" area in the mask. The 000 are dumped


Just as example:
Assume layer 23 is set

 0000 0000 1000 0000 0000 0000 0000 0000
 // since the number is greater than 65536 we strip the lower 16 bits and layer is set to "16"
 0000 0000 1000 0000
 // the value is not larger than 255 so we skip that part and implicitly strip the first 8 bits
 1000 0000
 // the value is larger than 15 so we strip the lower 4 bits and add 4 to the layer and get "20"
 1000
 // the value is larger than 3 so we strip the lower 2 bits and add 2 --> layer == 22
 10
 // bit 1 is set (value 2) so add another one to layer and end up at 23

For the layer 22 it would be exactly the same steps except we don't add 1 in the last step since the bits would be "01"

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

27 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 avatar image avatar image avatar image avatar image

Related Questions

Is it possible to allow a raycast to pass through a collider to hit things behind it? 6 Answers

Return Objects hit by raycast, check against a array of all objects? 1 Answer

some quesitions about layermask, culling mask, raycast. 0 Answers

Get LayerMask that current GameObject collides with? 1 Answer

Ignore one layermask question 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