Recap: Popup Timer
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 17 of 64.
This chapter connected scripts with events: C# events for code that listens, UnityEvents for rows wired in the Inspector, and OnEnable with OnDisable to listen only while active.
Challenge
EasyMake the achievement popup hide by itself.
- In
AchievementPopup, addprivate float timeLeft;and set it toshowTimeinShow. - In
Update(), whiletimeLeftis above 0, takeTime.deltaTimeoff it, and callpanel.SetActive(false)when it runs out.
Press Play and grab a coin: the popup hides 2 seconds later. Then Check.
Try it yourself
using UnityEngine;
using TMPro;
// Shows an unlocked achievement's title on the HUD.
public class AchievementPopup : MonoBehaviour
{
public GameObject panel;
public TMP_Text titleText;
public float showTime = 2f;
public void Show(string title)
{
titleText.text = "Unlocked: " + title;
panel.SetActive(true);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Architecture in Unity
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular TurretRecap: Twin Cannon4Serialized Data
Serializable ClassesLists in the InspectorEnums in the InspectorRecap: Loot TableRecap: Roll the Loot2Interfaces in Unity
IDamageableInterfaces and TryGetComponentGetComponentsRecap: Interact KeyRecap: Nearest in Reach3Events
C# Events in UnitySubscribe in OnEnableUnityEvent FieldsEvents Drive the UIRecap: Achievement PopupRecap: Popup TimerPractice on your own: Unity playground