Add some visual flair to your game with this simple yet powerful effect!

Unity Quick Tips: Fading an Object In and Out”

Part 4 of Our Bite-Sized Unity Tips and Tricks for Beginners

Valdarix Games
3 min readFeb 16, 2023

--

Welcome to Part 4 of our series of bite-sized Unity tips and tricks for beginners! In this article, we’re going to show you how to fade an object in and out in Unity. Whether you’re creating a game or another application, fading objects can add some visual flair and make your scene more dynamic.

Here’s the code sample:

using UnityEngine;

public class FadeObject : MonoBehaviour
{
private float alpha = 0f;
private float fadeSpeed = 0.5f;
private SpriteRenderer spriteRenderer;

private void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
}

private void Update()
{
alpha += fadeSpeed * Time.deltaTime;
spriteRenderer.color = new Color(1f, 1f, 1f, alpha);

if (alpha >= 1f || alpha <= 0f)
{
fadeSpeed = -fadeSpeed;
}
}
}
Fade code in action. May take a moment to start.

--

--

Valdarix Games
Valdarix Games

Written by Valdarix Games

Turning my passion for video games and software development experience into a focus on video game development.