x


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.

more ▼

asked May 11 '12 at 08:13 PM

Werewolf_Puppy gravatar image

Werewolf_Puppy
2 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered May 11 '12 at 08:31 PM

Berenger gravatar image

Berenger
11k 12 19 53

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x150

asked: May 11 '12 at 08:13 PM

Seen: 1067 times

Last Updated: May 11 '12 at 08:31 PM