- Home /
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).
You need to provide some details. Lists should have NO data at runtime.
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?
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.
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;
}
}
}
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
Follow this Question
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