- Home /
More Layers? Selecting one out of 50+ GameObjects?
Good morning,
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?
Drew
Answer by Bunny83 · Apr 04, 2012 at 06:25 PM
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.
now you should have all you need ;)
Thanks for the quick response, Bunny83.
I must admit, I could have read the documentation more... but after reading it as much as I have in the past week, I just felt like reaching out to someone that knew what they were doing to see if they had an answer ins$$anonymous$$d. I just started with Unity a few weeks ago, and it's been one heck of a ride so far!
I appreciate the links. I will look into that specific documentation now and go from there.
Answer by SirhotOP · Oct 10, 2021 at 10:01 PM
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.
Your answer
Follow this Question
Related Questions
Why Raycast hit a layer that I have removed from mask? 1 Answer
Brigade2 - Is it possible to implement Brigade2 path tracing engine into unity? 1 Answer
When i bring my mouse on layer mask my player doesn't move 0 Answers
What is the purpose of layers? 3 Answers
Check if Layer is in Layermask? 5 Answers