How to Use Sprite Renderer in Unity: Simple Guide
In Unity, use the
Sprite Renderer component to display 2D images (sprites) on GameObjects. Attach it to a GameObject, assign a sprite asset to the sprite property, and the image will appear in the scene.Syntax
The SpriteRenderer is a component you add to a GameObject to show a 2D image. Key parts include:
sprite: The image to display.color: Tint color applied to the sprite.flipXandflipY: Flip the sprite horizontally or vertically.sortingOrder: Controls which sprites appear in front when overlapping.
csharp
public class SpriteRenderer : Renderer { public Sprite sprite; public Color color; public bool flipX; public bool flipY; public int sortingOrder; }
Example
This example shows how to create a GameObject with a Sprite Renderer, assign a sprite, and change its color and flip it.
csharp
using UnityEngine; public class SpriteRendererExample : MonoBehaviour { public Sprite mySprite; void Start() { GameObject obj = new GameObject("MySpriteObject"); SpriteRenderer sr = obj.AddComponent<SpriteRenderer>(); sr.sprite = mySprite; // Assign the sprite asset sr.color = Color.green; // Tint the sprite green sr.flipX = true; // Flip horizontally sr.sortingOrder = 1; // Make sure it renders in front obj.transform.position = new Vector3(0, 0, 0); // Position in scene } }
Output
A green-tinted, horizontally flipped sprite appears at the center of the scene.
Common Pitfalls
Common mistakes when using Sprite Renderer include:
- Not assigning a sprite asset, so nothing shows.
- Forgetting to set the
sortingOrder, causing sprites to render behind others unexpectedly. - Using a sprite with transparent background but setting
colorto opaque black, hiding the image. - Placing the GameObject outside the camera view or at wrong Z position.
csharp
/* Wrong: No sprite assigned, so nothing appears */ SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>(); sr.sprite = null; // No sprite /* Right: Assign a sprite asset */ sr.sprite = mySprite;
Quick Reference
| Property | Description |
|---|---|
| sprite | The 2D image shown by the renderer |
| color | Tint color applied to the sprite |
| flipX | Flip the sprite horizontally |
| flipY | Flip the sprite vertically |
| sortingOrder | Render order priority for overlapping sprites |
Key Takeaways
Add a Sprite Renderer component to a GameObject to display 2D images.
Always assign a sprite asset to the Sprite Renderer’s sprite property.
Use sortingOrder to control which sprites appear on top.
Adjust color and flipX/flipY to customize sprite appearance.
Make sure the GameObject is within the camera view to see the sprite.