SpriteRenderer
- Game Development course
- Beginner
- Lesson 56/83
- 4 min
Learn to place a sprite in the scene with the SpriteRenderer.
SpriteRenderer component
Just as MeshRenderer displays objects in 3D, SpriteRenderer handles rendering in 2D. A GameObject requires this component to display a visible 2D graphic in the scene.
// Accessing SpriteRenderer and assigning sprite
SpriteRenderer sr = GetComponent<SpriteRenderer>();
sr.sprite = heroSprite;While the GameObject serves as a container, SpriteRenderer renders the assigned visual into scene pixels.
SpriteRenderer properties: Color and Flip
The color property on SpriteRenderer applies a tint color or adjusts transparency (alpha). The flipX and flipY options mirror the graphic via code to make characters face left or right.
// Flip character horizontally (face left)
sr.flipX = true;
sr.color = Color.white;These features eliminate the need to redraw identical sprites for opposite directional animations.
Practice Print renderer component state
Assign "SpriteRenderer" to renderer and "flipX" to flip. Print Component: SpriteRenderer | Flip: flipX to the console.
Starter code
using System;
class Program
{
static void Main()
{
// renderer ve flip değerlerini tanımlayıp yazdır
}
}Sample solution
using System;
class Program
{
static void Main()
{
string renderer = "SpriteRenderer";
string flip = "flipX";
Console.WriteLine("Component: " + renderer + " | Flip: " + flip);
}
}Expected output
Component: SpriteRenderer | Flip: flipX
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which component is used to render a selected Sprite visual on screen on a GameObject?
- Rigidbody2D
- SpriteRenderer Correct answer
- MeshFilter
- AudioListener
Explanation: SpriteRenderer is the component responsible for drawing 2D sprite visuals in scene space.
2 Flip visual horizontally
sr.____ = true;
sr.flipX = true;
Explanation: The flipX boolean property on SpriteRenderer is used to mirror the graphic horizontally.
3 SpriteRenderer fields
| Term | Meaning |
|---|---|
SpriteRenderer |
2D component rendering sprite in scene |
sprite |
Reference to 2D visual to render |
flipX |
Property mirroring visual horizontally |
color |
Field applying tint and transparency |