- Home /
Chaning a GameObjects Layer with C#
I want a GameObject to switch between two layers when a bool I have assigned to the GameObject switches between true and false.
private bool ACOG=false;
public GameObject ACOG_Obj;
When ACOG is true i want it to change to a different layer where it is visible. It is by default not visible, therefore it is default false.
This is probably super easy, but I am new to C# and programming in general and I can't figure this one out.
Answer by Berenger · May 11, 2012 at 08:31 PM
Layers are integers between 0 and 31. They are associated with a name for convenience and can be used as flags using bitwise operators : 1 << 0 | 1 << 1 etc.
So, if you want to change the layer of a gameobject, it will be something like gameObject.layer = 3. Of course, you don't want to know the layers by their numbers but by their names, so it would become gameObject.layer = LayerMask.NameToLayer( "Player" ).
As for the condition, I leave that to you.
I've already tried that, and for some reason it does not work for me. How do you do this in Unity 5?