- Home /
Is it possible to assign game objects to a layer through code at startup?
I am wandering it it is possible to assign objects to layers that have already been created with code.
My ultimate goal is to have some objects visible for each player in a multiplayer environment. More specifically I have battleships fighting in split screen. And I have long narrow planes that are children of the turrets that make it easier for the user to see exactly where the turrets are facing. And I want the plane that belongs to each user only to be visible in each user's camera and not the other.
I want to make plane1 belong to the player1 layer, which will be visible in player1's camera. and the same for player2. I will screen out the unwanted planes in the other cameras.
Thank in advance.
Answer by Eric5h5 · May 31, 2010 at 01:14 AM
Easiest way is probably NameToLayer:
object1.layer = LayerMask.NameToLayer("My Layer");
Answer by hawken-2 · Sep 19, 2011 at 03:24 AM
Does anyone know how this can be done to include the children of parent?
probably something along the lines of:
Transform parent = object1.transform;
foreach(Transform child in parent){ child.gameObject.layer = Layer$$anonymous$$ask.NameToLayer("$$anonymous$$y Layer"); }