Tutorial: How to create a timer in Unity 3D with JavaScript in Unity 5

This short written tutorial is aimed to show you in a few easy steps how to code a timer in Unity 3 D. You can easily modify the script by doing minor changes. This tutorial will make use of the new Unity UI System that was introduced in Unity 4.6f. Its also tested with Unity 5 and works just fine.

1. Step:Let’s get started by opening up a new or a existing unity project. Go ahead and add in a new GUI-Text by clicking GameObject, UI and finally choose Text (third option).

2. Step:Adjust the Render Mode of the automatically generated object called “Canvas”. Set it from Screen Space – Overlay to Screen Space – Camera. That way the timer will be placed perfectly fine according to our camera. You also have to set the Render Camera: Set it to your Main Camera.

3. Step:Click on Add Component at the bottom of the inspector panel (make sure you have Text GameObject selected) and select new Script. Then you have to give you script a proper name and set the language to Java Script.

4. Step: After Opening the newly generated Script (by default it will be stored in your assets folder) you should see this:

Replace everything thats currently in the script with the following code: