Unity - Simple second counter C# Tutorial

Let's get some games development for Unity 3D beginners, on this small tutorial I'll show you how to display a simple seconds timer for your Unity game.



Tried searching for a simple code to display seconds counter but there are nothing, here is one for sharing, the code bellow update the _textMesh with the seconds.

Second counter code:
public TextMesh _textMesh;
public float timer;
public int seconds;
private void update() {
  timer += Time.deltaTime * 10;
  seconds = Convert.ToInt32 (timer);
  _textMesh.text = seconds.ToString();
}


If you need a count down counter just do some changes as bellow:
public TextMesh _textMesh;
public float time;
public int seconds;
private void update() {
time -= Time.deltaTime * 10;
seconds = Convert.ToInt32 (time);
_textMesh.text = seconds.ToString();
if (seconds <= 0)
// do somthing here..
}

And if you need any more help, just leave a comment.

If you like this article, please share it on facebook, tweet it and +1 it to spread it :) !
Unity - Simple second counter C# Tutorial Unity - Simple second counter C# Tutorial Reviewed by Mhr on 12:08 Rating: 5

No comments

No Backlinks please, Comments are under moderation !