Bird
Raised Fist0
Unityframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of Sorting Layers in Unity?
easy
A. To apply physics properties to objects
B. To change the physical position of objects in the scene
C. To set the color of objects
D. To group objects and control their visual stacking order

Solution

  1. Step 1: Understand Sorting Layers concept

    Sorting Layers are used to group objects so Unity knows which objects appear in front or behind others visually.
  2. Step 2: Differentiate from other properties

    Sorting Layers do not move objects physically or change physics or color; they only affect visual stacking order.
  3. Final Answer:

    To group objects and control their visual stacking order -> Option D
  4. Quick Check:

    Sorting Layers = Visual stacking order [OK]
Hint: Sorting Layers control visual order, not position or physics [OK]
Common Mistakes:
  • Confusing sorting layers with object position
  • Thinking sorting layers affect physics
  • Assuming sorting layers change object color
2. Which of the following is the correct way to set the sorting order of a SpriteRenderer component in Unity C# script?
easy
A. spriteRenderer.orderSorting = 5;
B. spriteRenderer.sortOrder = 5;
C. spriteRenderer.sortingOrder = 5;
D. spriteRenderer.layerOrder = 5;

Solution

  1. Step 1: Recall SpriteRenderer property names

    The correct property to set the order within a sorting layer is sortingOrder.
  2. Step 2: Check each option for syntax correctness

    Only spriteRenderer.sortingOrder is a valid property; others are incorrect or do not exist.
  3. Final Answer:

    spriteRenderer.sortingOrder = 5; -> Option C
  4. Quick Check:

    Correct property name = sortingOrder [OK]
Hint: Use 'sortingOrder' property to set order in script [OK]
Common Mistakes:
  • Using incorrect property names like sortOrder
  • Confusing sorting layer with sorting order
  • Trying to set sorting layer with sortingOrder
3. Given two sprites in the same sorting layer with these orders:
spriteA.sortingOrder = 3;
spriteB.sortingOrder = 7;
Which sprite will appear on top visually?
medium
A. spriteB will appear on top
B. spriteA will appear on top
C. Both sprites will appear at the same level
D. Sorting order does not affect visual stacking

Solution

  1. Step 1: Understand sorting order within the same layer

    Higher sortingOrder values appear in front of lower ones within the same sorting layer.
  2. Step 2: Compare the given sortingOrder values

    spriteB has sortingOrder 7, which is higher than spriteA's 3, so spriteB appears on top.
  3. Final Answer:

    spriteB will appear on top -> Option A
  4. Quick Check:

    Higher sortingOrder = on top [OK]
Hint: Higher sortingOrder means sprite is drawn on top [OK]
Common Mistakes:
  • Thinking lower sortingOrder is on top
  • Confusing sorting layer with sorting order
  • Assuming sorting order has no effect
4. You set two sprites to the same sorting layer and sorting order, but one still appears behind the other unexpectedly. What is a likely cause?
medium
A. The sprite behind has a higher Z position in 3D space
B. Sorting layers override all other settings
C. Sorting order must be negative to appear on top
D. You must assign different sorting layers to fix this

Solution

  1. Step 1: Understand sorting layers and order limits

    Sorting layers and order control 2D visual stacking but Z position can still affect rendering if using 3D or perspective camera.
  2. Step 2: Identify cause of unexpected order

    If sorting layers and order are equal, the sprite with lower Z position (closer to camera) appears on top.
  3. Final Answer:

    The sprite behind has a higher Z position in 3D space -> Option A
  4. Quick Check:

    Z position affects draw order if sorting order is equal [OK]
Hint: Check Z position if sorting order is equal [OK]
Common Mistakes:
  • Assuming sorting layers always override Z position
  • Thinking sorting order must be negative
  • Believing different sorting layers are always required
5. You have three sprites: spriteX on sorting layer "Background" order 0, spriteY on sorting layer "Foreground" order 0, and spriteZ on sorting layer "Background" order 5.
Which sprite will appear on top visually?
hard
A. spriteZ will appear on top
B. spriteY will appear on top
C. spriteX will appear on top
D. spriteX and spriteY will appear on top together

Solution

  1. Step 1: Understand sorting layer priority

    Sorting layers have a fixed order set in Unity's editor. "Foreground" layer is above "Background" layer regardless of sortingOrder values.
  2. Step 2: Compare sprites across layers and orders

    spriteY is on "Foreground" layer order 0, which is above both spriteX and spriteZ on "Background" layer, even though spriteZ has order 5.
  3. Final Answer:

    spriteY will appear on top -> Option B
  4. Quick Check:

    Sorting layer priority beats sorting order [OK]
Hint: Sorting layer priority beats sorting order [OK]
Common Mistakes:
  • Thinking sorting order overrides sorting layer
  • Ignoring sorting layer hierarchy
  • Assuming higher order always means on top
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard