Fading the transparency of a material
A feature of many games is for objects to fade away to invisible, or appear gradually from invisible to fully visible. Unity provides the special Rendering Mode of Fade for exactly this purpose.
In this recipe, we will create an object that, once clicked, fades out and disappears. We'll also look at how to enahnce the code, to take account of the GameObject's own initial alpha value, to self-destruct when fading has finished and so on.
How to do it...
Follow these steps:
- Create a new 3D Sphere, named
Sphere-Game
. Choose menu:3D Object | Sphere
. - Select
Sphere-Game
and ensure it has a Collider (if you are using a custom 3D object, you might have to add a collider through menu: AddComponent | Physics | Box (or Mesh)
Collider). - Create a new Material named
m_fade
. - With the
m_fade
Material selected, change itsRendering Mode
toFade
in the Inspector:

Note
The Fade rendering mode is specifically designed for situations such as this. Other rendering modes, such as Transparent...