In this lesson, you will learn how to use Layers to control which objects your cameras can see and which physics objects can collide with each other.
What are Layers?
While Tags are useful for identifying a specific type of object (like "Player" or "Enemy"), Layers act as organizational buckets. In Unity, every GameObject belongs to a layer. By default, every object is in the Default layer, but you can assign them to specific categories to filter how the game engine interacts with them.
How to Assign a Layer
You can find the Layer dropdown menu at the very top right of the Inspector window when a GameObject is selected.
- Click the Layer dropdown.
- Select an existing layer, or click Add Layer... to define your own (e.g., "Enemies," "Environment," or "UI").
- Once assigned, you can use these layers to create rules for your game.
Why Use Layers?
Layers are primarily used for two tasks:
- Rendering: You can tell a Camera to ignore specific layers. For example, if you have a complex UI setup, you might want your main camera to ignore the "UI" layer so it only renders the game world.
- Physics Filtering: You can prevent certain objects from bumping into others. In the Project Settings > Physics window, you can view the Layer Collision Matrix. Here, you can uncheck the boxes where two layers intersect to stop them from ever colliding.
For example, if you have a "Player" layer and an "Item" layer, you can use a script to check for collisions:
// This logic could be used inside a script to detect
// objects only on a specific layer
int layerMask = LayerMask.GetMask("Items");
By organizing your project with layers early on, you prevent "everything-colliding-with-everything" bugs and keep your rendering pipeline efficient. Instead of checking every object in the scene to see if it should be visible or interactable, Unity uses these layers to instantly filter out the irrelevant data.
Key Takeaway: Layers provide a powerful way to filter the game world, allowing you to tell cameras what to render and physics components what to ignore.