Unity 3D Countdown script, using C#, for flash developers

Categories Unity 3D

First you need a GameObject GUI text, a GUI text is similar to flash TextField.  You create one, by going to GameObject>Create Other>GUI Text. GameObject is similar to DisplayObject in actionscript.

Scripts cannot exists without being assigned to a GameObject in the scene, there is no document class like in AS3. Luckily there is such a thing as an empty game object that we can use to assign scripts to it. To create one,  go to GameObject>Create Empty, name it whatever you want, mine is “TimerController”.

Creating our script:

Go to the Project panel, Assets and right click>Create>C# Script, you can name it whatever you want, mine is “CountdownTimer”. A file will be created in your assets folder with the extension .cs. (in our case “CountdownTimer.cs”).

Now we need to add our script to our empty game object “TimerController”, to do this, make sure your empty GameObject is selected (in the Hierarchy panel), then drag and drop your script (from the Project>Assets panel) to the Inspector panel. You can also assign a new script (called a Component in Unity), by selecting your GameObject, and clicking on your “Add Component” button, and in your search box, type your script name, and click it to assign it.

There is one thing we need to do before we jump to the countdown code, because we cannot access assign text to a GUI Text GameObject directly via the  guiText property (it is read only), like say someTextFiled.text = “my Text” in AS3, we need to add a public var to an assigned script of the GUI text we want to access (weird, I know).

Create a new script, I named it TextLink.cs, and add a “public string text” and assign it to guiText.text = text; in the update method. The script should look like:

  1. using UnityEngine;
  2. using System.Collections;
  4. public class TextLink : MonoBehaviour {
  6. public string text;
  7. // Use this for initialization
  8. void Start () {
  10. }
  12. // Update is called once per frame
  13. void Update () {
  15. guiText.text = text;
  17. }
  18. }

Coding our Countdown timer.

After you open your “CountdownTimer.cs”script in MonoDevelop, you see there are two methods, Start and Update. Start is similar to your init class method in AS3, and Update is similar to your EnterFrame event.

  1. using System;
  2. using System.Timers;
  4. public class CountdownTimer : MonoBehaviour {
  6. private GameObject timerTextfield;
  7. private int countTime;
  8. private Timer timer;
  10. // Use this for initialization
  11. void Start () {
  13. countTime = 120;
  15. timer = new Timer(1000);
  17. timerTextfield = GameObject.Find("CountdownTextfield");
  19. timer.Elapsed += new ElapsedEventHandler(TimerTick);
  20. timer.Start();
  22. }
  23. void TimerTick(object o, System.EventArgs e)
  24. {
  25. countTime--;
  26. Debug.Log(countTime);
  28. }
  29. // Update is called once per frame
  30. void Update () {
  32. if (countTime > 0) {
  34. } else {
  36. timer.Stop();
  38. }
  40. TimeSpan t = TimeSpan.FromSeconds (countTime);
  42. timerTextfield.GetComponent ().text = string.Format ("{0:0}:{1:00}", t.Minutes, t.Seconds);
  44. }
  45. }

One major difference we see, is that we cast the var type before the var name, so while in AS3 we would have:


  1. private var countTime:int;

is c# we have:

  1. private int countTime;

The timer is similar to what we are used to in AS3, one difference, is in the way it fires the event:

  1. Timer timer = new Timer(1000);
  3. timer.Elapsed += new ElapsedEventHandler(TimerTick);
  4. timer.Start();
  6. void TimerTick(object o, System.EventArgs e)
  7. {
  8. //do something
  10. }

Were TimerTick is the the method it fires after the time has passed. For more info see the Timer class.

Were in AS3 we would have:


  1. var timer:Timer = new Timer(1000, 1);
  2. timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
  3. timer.start();
  4. private function onTimerComplete(e:TimerEvent):void
  5. {
  6. //do something
  7. }


One last thing we have in our countdown, is the TimeSpan. This class, makes working with dates a little bit easier than in AS3. See the full docs at: timespan

In this countdown, it simply creates a date from the number of seconds we provide. This removes the trouble of converting seconds to minutes, or hours, days, etc.

On our last line, we display the countdown:

  1. timerTextfield.GetComponent<TextLink> ().text = string.Format ("{0:0}:{1:00}", t.Minutes, t.Seconds);

If you want to modify a GameObject component public vars and methods from another script.

First you need your GameObject name to locate it,  using  GameObject.Find(“NameOfGameObject”), which can be assigned to a variable:

  1. GameObject myObject = GameObject.Find("NameOfGameObject");

Second you need the component name (the script name) of that game object:

  1.   myObject .GetComponent<ScriptName> ().somePublicVariable

To fire public methods:

  1.   myObject .GetComponent<ScriptName> ().methodName()

To remember, the name of the GameObject is inside () and surrounded by “”, and the name of the script is inside <>, with no quotes.

In one line, our example would look like:

  1. GameObject.Find ("CountdownTextfield").GetComponent&lt;TextLink&gt; ().text = string.Format ("{0:0}:{1:00}", t.Minutes, t.Seconds);


It does take a little bit to get used to how Unity and C# works, but it is worth the time, Unity 3D is pretty awesome.


Share this

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *