Version: Unity 6.0 (6000.0)
Language : English
Change the sorting order of 2D GameObjects
Sorting Group component reference

Prevent 2D GameObjects mixing in sorting layers

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 Inspector 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.
A nested sorting group, its parent sorting group, and a third sorting group.

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.
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.
Two instances of the prefab without a sorting group. The sprites render incorrectly, because the body parts are all on the same sublayers.
Two instances of the prefab without a sorting group. The sprites render incorrectly, because the body parts are all on the same sublayers.

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

Five instances of the prefab with a sorting group. The sprites render correctly.
Five instances of the prefab with a sorting group. The sprites render correctly.

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

Additional resources

Change the sorting order of 2D GameObjects
Sorting Group component reference