- Home /
Unity Countdown timer in C#
I am creating a level of puzzle game in which user have to connect the red wire and and green wire. In wires at corner there are various shapes Like L-Shape , T-Shape and I-Shape pipe. I am rotaing the pipe of green wire and if green wire connection success then it should start timer say 20 seconds then user get only 20 seconds to connect red wire otherwise gameover. So please help me in timer.
Assume that Green wire connection is succesful.
GameMonitor.cs
using UnityEngine;
using System.Collections;
public class GameMonitor : MonoBehaviour {
GTShape1 GTS1;
// Use this for initialization
void Start () {
}
void Awake()
{
GTS1 = GameObject.FindGameObjectWithTag ("GTShape1").GetComponent<GTShape1> ();
}
// Update is called once per frame
void Update () {
if (GTS1.success) {
print ("Green Wire Connected");
}
}
}
Answer by Ben-Stoneman · Sep 12, 2014 at 08:51 PM
This should do it!
using UnityEngine;
using System.Collections;
public class GameMonitor : MonoBehaviour {
GTShape1 GTS1;
// Use this for initialization
void Start () {
}
void Awake()
{
GTS1 = GameObject.FindGameObjectWithTag ("GTShape1").GetComponent<GTShape1> ();
}
// Update is called once per frame
void Update () {
if (GTS1.success) {
print ("Green Wire Connected");
StartCoroutine(Timer());
print ("Time is Up!");
}
}
IEnumerator Timer ()
{
yield return new WaitForSeconds(20.0f);
}
}
For more help: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines
Your answer
Follow this Question
Related Questions
Making a sped up timer in unity 1 Answer
Problem with Snake Game(input help) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
animation control problem 0 Answers