0
0
Unityframework~10 mins

Sorting layers and order in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sorting layers and order
Assign Sorting Layer
Assign Order in Layer
Render Objects by Layer
Within Layer, Render by Order
Final Visual Stack on Screen
Objects get a sorting layer and an order number; Unity draws layers first, then orders within layers to decide what appears on top.
Execution Sample
Unity
sprite1.sortingLayerName = "Background";
sprite1.sortingOrder = 0;
sprite2.sortingLayerName = "Foreground";
sprite2.sortingOrder = 0;
sprite3.sortingLayerName = "Foreground";
sprite3.sortingOrder = 1;
Assign sorting layers and orders to three sprites to control which appears on top.
Execution Table
StepObjectSorting LayerOrder in LayerRender PriorityVisual Result
1sprite1Background0LowestDrawn first, behind others
2sprite2Foreground0MiddleDrawn after Background, behind sprite3
3sprite3Foreground1HighestDrawn last, appears on top
4End---Rendering complete with sprite3 on top
💡 All objects assigned layers and orders; rendering done in layer then order sequence.
Variable Tracker
ObjectSorting LayerOrder in Layer
sprite1Background0
sprite2Foreground0
sprite3Foreground1
Key Moments - 2 Insights
Why does sprite3 appear on top even though sprite2 and sprite3 share the same layer?
Because sprite3 has a higher order in the same sorting layer (1 vs 0), so it renders after sprite2, appearing on top (see execution_table step 3).
What happens if two objects have the same sorting layer and order?
Unity renders them in an undefined order, usually based on their position in the scene hierarchy or draw call order, which can cause flickering or unexpected overlap.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which sprite has the lowest render priority?
Asprite2
Bsprite1
Csprite3
DNone
💡 Hint
Check the Render Priority column in execution_table rows 1-3.
At which step does the object with the highest order in the Foreground layer get rendered?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Order in Layer and Render Priority columns in execution_table.
If sprite2's order is changed to 2, how does that affect the visual result?
Asprite2 will appear on top of sprite3
Bsprite3 will still appear on top
Csprite1 will appear on top
DNo change in rendering order
💡 Hint
Higher order in the same layer means drawn later and appears on top (see execution_table step 3).
Concept Snapshot
Sorting layers group objects for rendering order.
Within a layer, order in layer decides draw priority.
Higher layers draw over lower layers.
Higher order in layer draws over lower order.
Use sorting layers and order to control visual stacking.
Full Transcript
In Unity, sorting layers and order control which objects appear on top visually. Each object gets a sorting layer name and an order number. Unity draws all objects in lower layers first, then higher layers. Within the same layer, objects with lower order numbers draw first, and higher order numbers draw on top. For example, sprite1 is in the Background layer with order 0, so it draws first. Sprite2 and sprite3 are in the Foreground layer, but sprite3 has order 1, so it draws after sprite2 and appears on top. If two objects share the same layer and order, their draw order is undefined and can cause flickering. Changing order numbers changes which object appears on top within the same layer.