Build a Reusable Timer in Unity to Control Explosions (and other fun stuff too…)

Jared Halpern
12 min readApr 1, 2019

What We’re Building

In this post, we’re going to learn how to build a Timer that does something when the time elapses. The mechanism to do something will be customizable and generic enough so that you can use it to programmatically execute basically anything you can script. In the example we build, we will script the explosion animation to appear and play when the time runs out. We’ll build this Timer as a standalone script which can be attached to any GameObject in Unity and reused. You’ll also learn a bit about Coroutines and Interfaces too.

What you need to already know

• Familiarity with C# variables, classes, methods, the Unity Editor, GameObjects, Prefabs

• How to add new scripts to GameObjects, work with the Inspector

• Creating new Animation States in an Animation Controller

If you’re shaky on any of these points, don’t worry. We’ll walk through each step and if something still isn’t clear, feel free to ask in the comments.

The end result will look like the below gif except the explosion will only go off once. The little pirate gnome is just hanging out and watching the explosion. It’s not part of this tutorial:

Probably singed his eye-patch but didn’t flinch.

--

--

Jared Halpern

iOS Engineer • Game Developer • Author: Developing 2D Games with Unity. https://amzn.to/2Ln0jWn (2018 Apress Publishing) • https://jaredhalpern.com