# Prevent 2D GameObjects mixing in sorting layers

> Keep groups of 2D GameObjects from mixing together on the same sorting layers and sublayers.

To keep a group of 2D GameObjects from mixing with another group of objects on the same sorting layers and sublayers, add the GameObjects to a sorting group.

For example, you can use this approach to keep multiple instances of the same prefab from mixing with each other, even if they use the same sorting layers and sublayers.

Follow these steps:

1. In the **Hierarchy** window, create your group of 2D GameObjects, and set their **Sorting Layer** and **Order in Layer** values to sort them.
2. Select the GameObject at the top of the hierarchy of the group.
3. In the Editor window, select **Add Component** > **Rendering** > **Sorting Group**.
4. In the **Sorting Group** component, set the **Sorting Layer** and **Order in Layer** values.

The sorting order remains the same, but all the child GameObjects now render on the sorting layer and sublayer of the Sorting Group component.

> **Note:**
>
> Unity ignores the **Distance to Camera** property of each child GameObject. Unity calculates the distance based on the position of the GameObject that has the Sorting Group component.

## Nest sorting groups

You can nest sorting groups. A nested sorting group is sorted first, then sorted as a single item within the parent sorting group.


**A nested sorting group, its parent sorting group, and a third sorting group.:**
![](/api/media?file=/engine/6000.7/media/images/SG_diagram2.png)

To move a nested sorting group to the top level of the hierarchy without moving it in the **Hierarchy** window, select the sorting group then set **Sorting Type** to **Sort At Root**.

## Example

In the following example, the sprites belong to the same **Sorting Layer**, but have different **Order in Layer** values. When you create a prefab from the hierarchy, the sprites from multiple prefab instances are on the same layers and can mix with each other.


**A prefab of a zombie character. The hierarchy represents the different Order in Layer values of the different body parts. For example, the arms are rendered in front of the body. The character looks correct.:**
![](/api/media?file=/engine/6000.7/media/images/character_hierarchy.png)


**Two instances of the prefab without a sorting group. The sprites render incorrectly, because the body parts are all on the same sublayers.:**
![](/api/media?file=/engine/6000.7/media/images/part_intersect.png)

If you add a sorting group, the prefab instances render correctly.


**Five instances of the prefab with a sorting group. The sprites render correctly.:**
![](/api/media?file=/engine/6000.7/media/images/unique_orderlayer.png)

If mixing still occurs, set the **Order in Layer** value to a different value for each sorting group.

## Additional resources

* [2D rendering order](/engine/6000.7/manual/unity2d/sprite/sort-sprites/sort-sprites.md)
