How to toggle on or off a single layer of the camera's culling mask?

So in C# I’m trying to figure out how to just toggle the 9th layer.

So here’s the just of what I’m looking for…
if true
{
Camera.cullingMask actives layer 9
}
else
{
Camera.cullingMask deactivates layer 9
}

I need it to NOT interfere with the other layers that are currently activated or deactivated, I need it to toggle the 9th layer specifically.

As always, any help is appreciated.

PS. How the heck do you use “And” within an “If” statement?
I’ve tried things like
if varx == true && vary == true
but it treats it like I’m making an “Or”. Today’s just not my day :frowning:

In c# you can do 2 diferent connections in if statements “&&” - which is “AND” and “||” -which is “OR”
In your example you have to do it like so:

If((varx == true)&&(vary == true))
{
     //Do stuff
}

The first idea i have is to use something like:

var layerObjects = FindGameObjectsWithLayer(layer);

So you have a variable which stores all the required GameObjects so you can work with them.
Then you can:

foreach(var go in layerObjects)
{
    go.activeSelf  = false; // or true
}

It seems i missunderstood you. Then you can try to modify this:

 int oldMask = camera.cullingMask;
// change mask
camera.cullingMask = (1 << LayerMask.NameToLayer("TransparentFX")) | (1 << LayerMask.NameToLayer("OtherLayer"));
// do something
// ...
// restore mask
camera.cullingMask = oldMask;

Turn on and off the culling mask:

 // Turn on the bit using an OR operation:
private void Show() {
     camera.cullingMask |= 1 << LayerMask.NameToLayer("SomeLayer");
}
// Turn off the bit using an AND operation with the complement of the shifted int:
private void Hide() {
     camera.cullingMask &=  ~(1 << LayerMask.NameToLayer("SomeLayer"));
}
// Toggle the bit using a XOR operation:
private void Toggle() {
     camera.cullingMask ^= 1 << LayerMask.NameToLayer("SomeLayer");
}

Modify this for your needs.
Cheers!

8 Likes

Oh I needed the extra set of parenthesis for it, lol.

I’m not entirely sure what you’re getting at with the main question though.
Edit
Sorry miscommunication I suppose. I’m not looking to change the layers of any object. I’m looking to change what layers my camera is able to see. So to just turn a single cullingmask filter on or off without disturbing the other filters.
Edit 2
Alright thanks. That really looks intimidating but I’ll sit down tomorrow and really try to wrap my head around it. Cheers.

1 Like

Great. I hope you find a way to make it working. If you have any problems just ask me here. Cheers!

1 Like

Well I suppose as long as you’re here…
Could you explain what the “1” and the “<<” mean?
I’ve used them other areas but really it’s just trial and error.

I will be back in 2 hours I will answer you anything. Sorry I have to go now.

Layermask is a ‘bitmask’
1 << x is a bitwise shift

to toggle a bit on and off, use XOR
camera.cullingMask = camera.cullingMask ^ (1 << 9) (to toggle layer 9)

3 Likes

Thanks, I really love how much better at Unity everybody else is compared to me. Makes it really easy to learn from others when there is a knowledgeable community.

Edit
Being a huge pain here I know, but could I get something that specifically turn it “On” and one that turns it “Off”. I know I asked for a toggle, I actually need the ability to turn it “On” (Even if it is on) or Off.

Hey, I am back but I see you already have the answer. Good luck. (I edited the original for your needs.)
[“code=CSharp”] It looks better [“/code”]

Like this.
1 Like

Thanks everybody, my project is really starting to take shape now :slight_smile:

This is what I used to turn layer 4 on and off:

sunLight.cullingMask = ~ (1 << 4) ;

sunLight.cullingMask |= (1 << 4); //note pipe needs to be NEXT right to equals with no space!

There’s a post on Gamasutra that shows how to do it

3 Likes