Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"A game object can only be in one layer" error in Unity

I am trying to make a MMO game with Unity 4.2 and I can built it successfully but when I play into Unity I got several warnings like for example:

A game object can only be in one layer. The Layer need to be in the range[ 0....31] UnityEngine.Gameobject: set_layer(int32)

Could anybody help me to fix these ones?

like image 588
Alejandro Castan Avatar asked Oct 22 '25 05:10

Alejandro Castan


2 Answers

It seems that GameObjects and LayerMasks work differently and the error thrown is wrong. Use bitshifting tricks (1 << 13) for raycasts/layermasks and the shiftcount (13) for the gameobjects (.layer), I know it doesn't make sense, but it's different for both. I just wasted half an hour with this.

like image 168
Renzo Coppola Avatar answered Oct 26 '25 04:10

Renzo Coppola


Layers in Unity are defined as an integer mask. What this in essence means is that they are defined inside the engine by an integer value where a single bit is set.

So if you set your layer to have a value of 0, the actual mask integer value is 1 << 0. Or if you set it to be 2, the actual integer mask value stored is 1 << 2. It should not be too hard to imagine why you can then only have 32 distinct layers. There are 32 bits in an integer after all.

So check what you set your layers to and see if they are within range.

like image 45
Bart Avatar answered Oct 26 '25 05:10

Bart