I am coding a board game in c# and have all of the code I need to handle the touch input of my board game pieces, except that I only seem to have 31 Layers to use, therefore my 31 layermasks to use for my ray tracing. My game uses more than 50 pieces, and I would like them all to be touchable at one point or another.
Is it possible to remove a Gameaobject from a layer and add others to a Layer via code? (ie: I would have a selectable piece layer and manually assign all the pieces I want to be able to touch to that layer.)
Assuming #1 is true, is it possible to return a Gameaobject value from a ray trace? This would cut down the layers I need to use dramatically!
If the first two aren’t possible, am I able to use more layers somehow?
It isn’t possible to use more layers. The layermask is an integer bit mask. Since an integer has 32 bits you have 32 layers.
If you had took a look into the documentation you would have found GameObject.layer which allows you to get or set the layer of a GameObject.
Also if you read the docs on Physics.Raycast carefully you will notice that there are different versions of this function. Some will return information in the RaycastHit struct you pass by reference.
I am pretty late but maybe I can be helpful someone else.
In your case as Bunny83 says its not possible to use more than 32 layers. Already you do not want to spend your layers for your characters. instead, I would suggest to use “tags” with oncollisionenter/exit/stay or ontriggerenter/exit/stay. so you will have unlimited options. for sure your code gotta change.