0
0
Unityframework~20 mins

Sorting layers and order in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting Layers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of sorting order values?
Consider two GameObjects with SpriteRenderer components. One has sortingLayerName = "Background" and sortingOrder = 2, the other has sortingLayerName = "Foreground" and sortingOrder = 1. Which one will appear on top in the game view?
Unity
SpriteRenderer bgRenderer = backgroundGameObject.GetComponent<SpriteRenderer>();
SpriteRenderer fgRenderer = foregroundGameObject.GetComponent<SpriteRenderer>();

// Background layer order
Debug.Log($"Background layer order: {bgRenderer.sortingOrder}");
// Foreground layer order
Debug.Log($"Foreground layer order: {fgRenderer.sortingOrder}");
AThe Background object appears on top because its sortingOrder is 2.
BBoth objects appear at the same level because sortingOrder values are ignored when sortingLayerNames differ.
CThe Foreground object appears on top because its sortingLayerName is "Foreground" which is above "Background" regardless of sortingOrder.
DThe Background object appears on top because sortingLayerName "Background" has higher priority than "Foreground".
Attempts:
2 left
💡 Hint
Remember that sorting layers have a priority order set in Unity, and sortingOrder only affects order within the same layer.
Predict Output
intermediate
1:30remaining
What is the sorting order after changing it at runtime?
You have a SpriteRenderer with sortingOrder = 5. You run this code: spriteRenderer.sortingOrder = 10; What is the sortingOrder value after this code runs?
Unity
spriteRenderer.sortingOrder = 10;
Debug.Log(spriteRenderer.sortingOrder);
A10
BIt throws an error because sortingOrder is read-only.
C0
D5
Attempts:
2 left
💡 Hint
Think about whether sortingOrder can be changed at runtime.
Predict Output
advanced
2:30remaining
What is the output of this sorting layer comparison code?
Given two SpriteRenderers with the same sortingOrder but different sortingLayerIDs, what does this code print? ```csharp int result = spriteRendererA.sortingLayerID.CompareTo(spriteRendererB.sortingLayerID); Debug.Log(result); ```
Unity
int result = spriteRendererA.sortingLayerID.CompareTo(spriteRendererB.sortingLayerID);
Debug.Log(result);
AAlways 0 because sortingLayerID is the same for all layers.
BAlways positive because sortingLayerID is a unique positive number.
CThrows a runtime error because sortingLayerID is not comparable.
D0 if both are on the same sorting layer, positive if A's layer is above B's, negative if below.
Attempts:
2 left
💡 Hint
sortingLayerID is an integer representing the layer's priority.
Predict Output
advanced
2:30remaining
What happens if two objects share the same sorting layer and order?
Two GameObjects have SpriteRenderers with sortingLayerName = "Default" and sortingOrder = 0. What determines which one is drawn on top?
AThe one created last in the scene hierarchy is drawn on top.
BThe one with the lower Z position is drawn on top.
CThe draw order is undefined and may flicker between frames.
DThe one with the higher instance ID is drawn on top.
Attempts:
2 left
💡 Hint
When sorting layers and orders are equal, Unity uses the object's position in 3D space.
🧠 Conceptual
expert
3:00remaining
How to ensure UI elements always appear above sprites using sorting layers?
You want UI elements to always appear above all sprites in your scene. Which approach ensures this reliably?
ASet UI Canvas sortingLayerName to a layer above all sprite layers and set sortingOrder high enough.
BSet UI Canvas sortingLayerName to "Default" and sortingOrder to 0.
CChange the Z position of UI elements to be higher than sprites.
DUse the same sorting layer as sprites but set sortingOrder to a negative number.
Attempts:
2 left
💡 Hint
Think about sorting layers priority and how UI Canvas uses them.