- Home /
Problem resolved. Not a coding issue! Thanks robertbu
OverlapSphere, Layermask problems
I can't work out why my layermask isn't working. If I leave it out of the OverlapSphere the function works correctly. With it nothing is returned. All objects are correctly layered and I have tried | instead of || in the layermask.
Layermasks :
var layerBlock1 : int = 11;
var layerBlock2 : int = 12;
var layerBlock3 : int = 13;
var layerBlock4 : int = 14;
var layerBlock5 : int = 15;
var layerMaskTotal : int = ((1 << layerBlock1) || (1 << layerBlock2) || (1 << layerBlock3) || (1 << layerBlock4) || (1 << layerBlock5));
Overlap code:
var hitColliders : Collider[] = Physics.OverlapSphere(transform.position, 10, layerMaskTotal);
for (var col : Collider in hitColliders)
{
Debug.Log(col.transform.gameObject.name);
}
Any suggestions are greatly appreciated!
Answer by robertbu · Aug 22, 2013 at 02:30 PM
The operator is a single '|' symbol, not a logical or ('||' ):
var layerMaskTotal : int = ((1 << layerBlock1) | (1 << layerBlock2) | (1 << layerBlock3) | (1 << layerBlock4) | (1 << layerBlock5));
Edit: This comment marked the answer :
These are all public variables, so after the first time they are set, they take their value from the inspector. So check the value in the inspector. Given what you are doing, I suggest you make them all private:
private var layerBlock1 : int = 11;
Thanks for the reply robertbu; Aye, that's what I read. I've tried it both ways and the results are the same. I'm baffled
Do a Debug.Log() on layer$$anonymous$$askTotal. What is the value?
2048 (In both cases | and ||) This was for an updated layermask with an extra layer 16 in there. When I removed it the value was still 2048.
It should be 31744, right?
These are all public variables, so after the first time they are set, they take their value from the inspector. So check the value in the inspector. Given what you are doing, I suggest you make them all private:
private var layerBlock1 : int = 11;
For Javascript (which you are using), variables are public by default. In C# they are private by default.
If you see them in the inspector (and haven't done anything to hide them from the inspector), they are public.
Follow this Question
Related Questions
OverlapSphere doesn't detect colliders 1 Answer
Does adding a Layermask improve efficieny of Physics.OverlapSphere? 1 Answer
OverlapSphere ignoring all colliders when I use the layerMask parameter. 1 Answer
Replacing OverlapSphere with colliders (Detecting weapon collision with enemies - SOLVED) 1 Answer
OverlapCircleAll, layerMask ( Problem) 2 Answers