0
0
Unityframework~10 mins

Sprite Renderer component in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sprite Renderer component
Add Sprite Renderer to GameObject
Assign Sprite Image
Set Color and Flip Options
Adjust Sorting Layer and Order
Sprite Renderer displays sprite in Scene
The Sprite Renderer component shows a 2D image on a GameObject by assigning a sprite and setting display options.
Execution Sample
Unity
var sr = gameObject.AddComponent<SpriteRenderer>();
sr.sprite = mySprite;
sr.color = Color.red;
sr.flipX = true;
sr.sortingOrder = 5;
This code adds a Sprite Renderer, sets its sprite image, changes color to red, flips it horizontally, and sets its draw order.
Execution Table
StepActionProperty ChangedValue SetEffect
1Add SpriteRenderer componentComponent addedSpriteRendererGameObject can now display a sprite
2Assign sprite imagespritemySpriteSprite image is set to display
3Change colorcolorredSprite color changes to red tint
4Flip horizontallyflipXtrueSprite is flipped left-right
5Set sorting ordersortingOrder5Sprite draws above lower order sprites
6Render frameDisplaySprite with red tint, flipped, order 5Sprite visible in scene with all settings
💡 All properties set, Sprite Renderer displays sprite with specified settings
Variable Tracker
PropertyInitialAfter Step 2After Step 3After Step 4After Step 5Final
spritenullmySpritemySpritemySpritemySpritemySprite
colorwhitewhiteredredredred
flipXfalsefalsefalsetruetruetrue
sortingOrder000055
Key Moments - 3 Insights
Why does the sprite not show if I forget to assign the sprite property?
Without assigning the sprite (see step 2 in execution_table), the Sprite Renderer has no image to display, so nothing appears.
What happens if I set flipX to true after setting the sprite?
As shown in step 4, flipX flips the sprite horizontally without changing the sprite image itself.
How does sortingOrder affect which sprite appears on top?
Sprites with higher sortingOrder values (step 5) draw over sprites with lower values, controlling visual layering.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the color property after step 3?
Awhite
Bblue
Cred
Dnull
💡 Hint
Check the 'color' property value in the variable_tracker after step 3
At which step does the sprite get flipped horizontally?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'flipX' property change in the execution_table
If sortingOrder was set to 0 instead of 5, what would change?
ASprite would not be visible
BSprite would draw below sprites with sortingOrder 5
CSprite color would change
DSprite would flip vertically
💡 Hint
Refer to the explanation of sortingOrder effect in key_moments and execution_table step 5
Concept Snapshot
Sprite Renderer component:
- Attach to GameObject to show 2D images
- Assign sprite property to set image
- Use color to tint sprite
- flipX/flipY to flip image
- sortingLayer and sortingOrder control draw order
- Changes appear immediately in scene
Full Transcript
The Sprite Renderer component in Unity lets you display 2D images on GameObjects. First, you add the component to your GameObject. Then you assign a sprite image to the sprite property. You can change the color to tint the sprite, flip it horizontally or vertically using flipX or flipY, and control which sprites appear on top by setting sortingLayer and sortingOrder. Each step changes the component's properties, and the sprite updates visually in the scene. If you forget to assign a sprite, nothing will show. Flipping changes the image orientation without changing the sprite itself. Sorting order determines layering when multiple sprites overlap.