Game Clear Screen

How to Fade In Text

Tristan Engel
2 min readAug 5, 2021

After defeating the boss we want to display the game clear text. Our boss music stops when the boss is defeated. After a few seconds when the game clear text is on screen the game cleared music starts.

BossHealth Script

In our script that handles our BossDeath we added some code. We grant some score for killing the boss. Then we instantiate the gamecleartext prefab and set the parent as canvas.

private void BossDeath()
{
_player.AddScore(100);
GameObject gameClearText = Instantiate(_gameClearText, _canvas.transform.position, Quaternion.identity);
gameClearText.transform.SetParent(_canvas.transform);

GameClearText Script

This script is on the prefab that gets instantiated when the boss is killed.
Using AudioSource.PlayDelayed we start the game clear music that is on the GameClearText gameobject.
The FadeIn() coroutine fades the game clear text in.

//handle
private AudioSource _audiosource;
private Text _text;

//config
private float _BGMduration = 5f;
private float _fadeDuration = 5f;

void Start()
{
_audiosource = GetComponent<AudioSource>();
_audiosource.PlayDelayed(_BGMduration);

_text = GetComponent<Text>();

StartCoroutine(FadeIn());
}

private IEnumerator FadeIn()
{
float currentTime = 0f;
while (currentTime < _fadeDuration)
{
float alpha = Mathf.Lerp(0f, 1f, currentTime/_fadeDuration);
_text.color = new Color(_text.color.r, _text.color.g, _text.color.b, alpha);
currentTime += Time.deltaTime;
yield return null;
}
yield break;
}

--

--

Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.