This tutorial will guide you on how to make a simple object be able to fade out in Unity. We will be coding this in C#. This class is great for when an enemy dies and you want him to fade out off screen and then vanish and clean it up. This way you can get rid of bullets, enemy pieces, bodies and more with such a simple powerful class. Anything that spawns and needs to vanish immediately.
You can download the .cs class here on my Github
You can download the class from there and drag and drop it into your project onto any sprite that you want to fade out automatically when it's on screen. Set the deathremovetimer in the inspector to whatever time you want it to last before fading out.
First lets look at the full code below -
The code works by setting our colour variable to the spriteRenderer of our object we place the class on. Then it begins counting down in deltatime. If the timer is below the deathremovetimer. For example if we set it to 3 seconds, after 3 seconds it will run the code to fade out.
we do this by getting the alpha with mycolour.a and setting it to the new int which is colourfade that keeps decreasing.
when the colourfade gets below 0 and you can't see it anymore it will run the Destroy code to remove it.
We start off by setting our variables.
public float deathremovetimer;
This code is made public because we want to be able to change the number in the inspector. We set it to float because it needs to be a number that can use point digits like 0.1, 0.2 etc and then we name it.
private Color mycolour;
This uses Unitys built in Colour variable so we can set our variable to it.
private float colourfade = 1;
Since we want our fade to start at full visibility and count backwards, we need to set it to 1. A 1 basically means 100% in C#
Now in our code that runs as soon as the game begins we need to execute the following
mycolour = GetComponent<SpriteRenderer> ().color;
This sets our Color property Unitys built in spriteRenderer that we attach the class too. So if we put it on a ball, it will look for the balls sprite renderer that displays the ball and it will now target that sprites color property.
mycolour.a = 1f;
We then set our Colour property immediately to 1f so it starts visible and we can begin.
Update will be running every frame of your game. So this will be constantly going so lets handle it appropriately.
deathremovetimer -= Time.deltaTime;
Now we use our variable timer to count backwards in seconds. It uses deltatime so that its accurate to real life. 3 seconds will be equal to 3 seconds in real life.
if (deathremovetimer <= 0) {
If the deathremovetimer is now below 0, the countdown as finished and we can begin our fadeout code.
colourfade -= 0.03f;
We start counting down our float, you can change this number to a higher number if you want it to fade out faster.
mycolour.a = colourfade;
Now we set the alpha of our sprite renderer to the float we deducted above, since it will be constant, this will keep updating our alpha class. If you are wondering why I didn't just do mycolour.a -= 0.03f it's because Unity can be a pain in the ass. That's why >_>
GetComponent<SpriteRenderer> ().color = mycolour;
Now that our alpha has been changed, we can update the sprite with the new information.
if (colourfade <= 0) {
Once the object is completely invisible we will execute the next line of code. You can change this number to 0.5 if you want to destroy it before its fully vanished.
Destroy (gameObject);
This will destroy the object on screen and remove it from our Unity Hierarchy. This is great for cleaning up a huge amount of objects on screen and removing them.
There you have it! A simple class we can drag and drop onto a bullet shell. Then whenever we spawn that bullet shell, a few seconds later it will fade out. Giving the player an enjoyable experience. I used this class a lot in my game Eggbot vs Zombies because there was constantly bodies that needed to be removed to stop lag.
Hope this class helps you code your own fadeouts!