- Home /
how to make timer in the game
hi, how to make a timer in the racing game,(i mean 3,2,1. GO!) THANK YOU....
Answer by Dreamside · Feb 22, 2012 at 04:15 PM
using UnityEngine;
using System.Collections;
public class Timer : MonoBehaviour
{
float currentTime = 0;
int countValue = 3;
bool gameStarted = false;
void Start()
{
currentTime = Time.time;
}
void Update()
{
if (!gameStarted)
{
if (Time.time - currentTime >= 1)
{
Debug.Log(countValue);
countValue--;
currentTime = Time.time;
}
if (countValue == -1)
{
gameStarted = true;
StartGame();
}
}
}
void StartGame()
{
Debug.Log("Game is started");
}
}
To which i should attach this script,i been tryed attaching to the player,empty game object and it is saying can't add script.
You should attach it any gameobject you want but you should name script as Timer.cs
Answer by James Tima · Feb 23, 2012 at 04:27 PM
U can use GUI Text go to Game Object --> Create Other --> GUI Text and apply this script
var TextTop : GUIText; // assign GUI Text
function ShowText()
{
TextTop.text = "3";
yield WaitForSeconds(1);
TextTop.text = "2";
yield WaitForSeconds(1);
TextTop.text = "1";
yield WaitForSeconds(1);
TextTop.text = "Go....";
}
am trying to assign GUI text in the text top.but it is not assigning please help.
here is the silly question : TextTop is declared public right?
i was trying to attach as shown in the script but it is not assigning,am new to the unity i don't know much about it.
go to GameObject --> CreateOther -->GUI Text then u saw a GUI Text on hierachy view and where u attach script there u saw TextTop u just drage GUITxt from Hierachy view to TextTop.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064343im_/https://answers.unity.com/themes/thub/images/avi.jpg)