Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by Reconnoiter · Dec 20, 2016 at 01:21 PM · inspectorlayerlayermask

layer = layerMask (which is set in Inspector)

Howdy,

I have want to set the layer for an gameObject in the inspector (instead of using a number or string in the code). So I added this to my object's script:

 [SerializeField] private LayerMask previewLayer;

And selected the right layer for it in the inspector.

And now in the object's c# script I want to set the layer of the GameObject to the previewLayer.

 gameObject.layer = previewLayer.value;
 or 
 gameObject.layer = previewLayer;

But I get this error: "A game object can only be in one layer. The layer needs to be in the range [0...31]". How to solve this? Tia

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 Sergio7888 · Dec 20, 2016 at 01:50 PM 1
Share

Layer$$anonymous$$ask is like a sequence of booleans which is converted to a int from int.$$anonymous$$inVale to int.$$anonymous$$axValue, is dierent from a layer which is a int from 0 to 31. I recomend you use:

[SerializeField]
[Range(0,31)]
private int previewLayer;
avatar image Reconnoiter Sergio7888 · Dec 20, 2016 at 02:03 PM 0
Share

Thanks for the information on Layer$$anonymous$$asks.

avatar image Reconnoiter Sergio7888 · Dec 20, 2016 at 02:23 PM 0
Share

I think your method in combination with constants might be the best way to do it.

avatar image Sergio7888 Reconnoiter · Dec 21, 2016 at 12:19 PM 0
Share

also if you need a drop down with the layer names you need create a custom editor

avatar image Reconnoiter · Dec 21, 2016 at 01:02 PM 0
Share

Why am I downvoted?

6 Replies

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

Answer by Reconnoiter · Dec 20, 2016 at 02:00 PM

Hi everyone, thanks for replying,

I think I found the answer in this thread https://forum.unity3d.com/threads/get-the-layernumber-from-a-layermask.114553/ .

I now use this little helper function to pass a LayerMask and get an int from it back (within a static class):

 public static int layermask_to_layer(LayerMask layerMask) {
         int layerNumber = 0;
         int layer = layerMask.value;
         while(layer > 0) {
             layer = layer >> 1;
             layerNumber++;
         }
         return layerNumber - 1;
     }

I think it works, if it appears to be buggy or such I will edit this answer.

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 G_hi3 · Nov 14, 2020 at 02:32 PM 0
Share

I made this function based on the code of your answer. It checks if a given layer matches any of the layers in the Layer$$anonymous$$ask object:

 private static bool IsLayerCollision(int layer, Layer$$anonymous$$ask layer$$anonymous$$ask) {
   return (1 << layer & layer$$anonymous$$ask.value) != 0;
 }

As far as I understand it, an object in Unity can only be on a single layer.

avatar image
8

Answer by tommynanny · Mar 28, 2020 at 09:48 PM

I know it is an old question, but in case someone still needs it. A much easier way to set layer from layer mask from the inspector:

int layer = (int) Mathf.Log(layerMask.value, 2);

:)

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 GrKl · Dec 20, 2016 at 01:36 PM

gameObject.layer = LayerMask.NameToLayer( "YourLayerName" );

or

gameObject.layer = x; (where x is an integer equal to your layer number)

Your solution does not work because LayerMask is a struct, not an int. Though I would have thought that as LayerMask.value returns an int that it should have worked. Except if your layerMask contained multiple layers

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 Sergio7888 · Dec 20, 2016 at 01:51 PM 0
Share

Layer$$anonymous$$ask can be implicit converted to int.

avatar image meat5000 ♦ · Mar 28, 2017 at 04:58 PM 0
Share

Layer$$anonymous$$ask a struct? Never heard of this. As far as I know its an int and can very simply be used for multiple layers.

You can directly interpret which Bits are on or off from a single decimal number.

http://www.rapidtables.com/convert/number/decimal-to-binary.htm

http://answers.unity3d.com/questions/1177883/overlapsphere-ignoring-all-colliders-when-i-use-th.html

http://answers.unity3d.com/questions/945534/help-with-layermask-and-raycast-help.html

http://answers.unity3d.com/questions/521568/overlapsphere-laymask-problems.html

http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html

avatar image
-1

Answer by IntergalacticSloth · Mar 28, 2017 at 04:39 PM

Another option. With this, you can at least type the NAME of the layer in the inspector.

  public string layerNameWeTypedInInspector;
 ...
  gameObject.layer = LayerMask.NameToLayer(layerNameWeTypedInInspector);

Then you see i set the layer of some gameObject.

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 james_1983cute · Sep 15, 2017 at 08:52 AM

as meat5000 said, As far as I know its an int and can very simply be used for multiple layers. You can directly interpret which Bits are on or off from a single decimal number. You can use this online tool for conversion http://www.calculatorology.com/decimal-to-binary-converter/

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
  • 1
  • 2
  • ›

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

70 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 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 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

Type for layer selection 3 Answers

OverlapSphere doesn't detect colliders 1 Answer

How to use LayerMask variables with conditionals? 1 Answer

Inspector has no layer and transform has no z-axis 1 Answer

How to create User layer that acts like the "Ignore Raycast" layer? 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