- Home /
LayerMasks are inconsistent between different types of Inspector control.
So I've got a script GlobalFlockPropertyManager that manages the properties for a large number of member scripts that all need to be identical. The scripts that I'm managing are things like SteerForAlignment, which come from the UnitySteer 2.2 package.
I'm trying to set a LayerMask from the script onto all of the GameObjects, and I'm getting some strange results. Using the inspector, I set the LayerMask on the GlobalFlockPropertyManager to "Neighbors". The script then propagates the LayerMask value to all of the scripts it's managing, but their LayerMasks appear in the inspector as "Default, Unnamed 3".
Using the debug log, I've confirmed that the LayerMask's int values are all 9, but they still report different names in the inspector.
However, if I set the individual member scripts' LayerMasks to "Neighbors", then the int value that they report is 512.
I'm using a custom inspector for GlobalFlockPropertyManager, so that might be causing it. When I use the drop-down list to select the layers in GlobalFlockPropertyManager, I see a bunch of layer options, plus "Add Layer". This control is created by:
Target.LayersChecked = EditorGUILayout.LayerField("\t\t\tLayers Checked", Target.LayersChecked);
When I use the drop-down lists for the member scripts, which are created by the default inspector, I see the same layer options, without "Add Layer", but with "Nothing" and "Everything" added.
Basically I want to understand why the int values and the reported layers are different for the two different inspector windows.
Answer by Novack · Jul 05, 2012 at 11:21 PM
The problem is that you need to get a bit mask to correctly apply a Layer.
From the Layer documentation:
// Bit shift the index of the layer (8) to get a bit mask
int layerMask = 1 << 8;
So in the case above, the layer index was 8, replace that for the layer int index you want to set and then you can change the layer.
I'm not sure how your answer explains the appearance of the layers in the inspector, or the inconsistency between different ways of applying a layer mask.
I'm not manually applying layer masks, I'm just copying them from one layer mask to another. I'm aware that the layer masks aren't actually meant to be read as ints, I'm just describing the int representation.