- Home /
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
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;
Thanks for the information on Layer$$anonymous$$asks.
I think your method in combination with constants might be the best way to do it.
also if you need a drop down with the layer names you need create a custom editor
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.
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.
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);
:)
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
Layer$$anonymous$$ask can be implicit converted to int.
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
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.
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/