- Home /
How to create a countdown counter based off of a Key press.
I have been taking my hand at creating a timer that countsdown when the letter 'f' is pressed. I only want it to count down when this letter is pressed, (EDIT: I mean this to mean that it starts a countdown when you click F, and resets either when the timer ends, or the player hits f) because I am creating a timer that when it reaches 0 the light turns off. (: I've tried creating a timer, so I will post it up!
private var timeLeft: float = 0;
var totalTime : float = 30;
function Awake(){
timeLeft = totalTime;
}
function Update(){
timeLeft -= Time.deltaTime;
if(timeLeft < 0)
{
light.enabled = false;
}
}
Thank you! (: Also, this is just excerpts from my code, not the entire thing. (:
Answer by clunk47 · Dec 04, 2013 at 07:02 PM
#pragma strict
private var timeLeft : int = 10;
private var counting : boolean = false;
function Start()
{
StartCoroutine(CountDown());
}
function Update()
{
if(Input.GetKeyDown(KeyCode.F))
counting = !counting;
if(timeLeft <= 0 && light && light.enabled)
light.enabled = false;
}
function CountDown()
{
while(true)
{
if(counting && timeLeft > 0)
timeLeft--;
yield WaitForSeconds(1);
}
}
//FOR TESTING PURPOSES
function OnGUI()
{
GUILayout.Label("Time Remaining - " + timeLeft.ToString() + "\n" + "Press 'F' Key to toggle timer.");
}
You have time always equal to 30 in update, you are asking if(timeLeft = 0) ins$$anonymous$$d of if(timeLeft == 0) or timeLeft
#pragma strict
var matchText : GUIText;
var match : int = 0;
var meshRen : $$anonymous$$eshRenderer;
var otherGO : GameObject;
var matchRend : $$anonymous$$eshRenderer;
var otherGameObject : GameObject;
var ray : Ray;
var hit : RaycastHit;
private var timeLeft : int = 30;
private var counting : boolean = false;
function Awake ()
{
meshRen = otherGO.GetComponent($$anonymous$$eshRenderer);
matchRend = otherGameObject.GetComponent($$anonymous$$eshRenderer);
matchRend.enabled = false;
matchText.text = "";
}
function Update ()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
if (Physics.Raycast(ray, hit, 100))
{ // 1000 or $$anonymous$$athf.Infinity should be enough !
// what did the raycast hit ?
Debug.Log( "ray hit (name): " + hit.collider.gameObject.name);
Debug.Log( "ray hit (tag): " + hit.collider.gameObject.tag );
if(hit.collider.gameObject.tag == "match")
{
meshRen.enabled = false;
matchRend.enabled = true;
Destroy(hit.collider.gameObject, 3);
match += 15;
matchText.text = "Aquired 15 matches.";
Destroy(matchText, 3);
}
}
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
{
counting = !counting;
timeLeft = 30;
}
if(timeLeft <= 0 && light && light.enabled)
light.enabled = false;
}
function Start()
{
StartCoroutine(CountDown());
}
function CountDown()
{
while(true)
{
if(match >= 0)
{
if(counting && timeLeft > 0)
{
timeLeft--;
yield WaitForSeconds(1);
}
}
yield WaitForEndOfFrame();
}
}
//FOR TESTING PURPOSES
function OnGUI()
{
GUILayout.Label("Time Remaining - " + timeLeft.ToString() + "\n" + "Press 'F' $$anonymous$$ey to toggle timer.");
}
//Not sure where you're going to be calling this.
function $$anonymous$$atchCounter()
{
if(match >= 1)
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
{
if(!light.enabled)
{
light.enabled = true;
match--;
}
else light.enabled = false;
}
}
}
I have this working without using your other script, so you may want to remove
if(timeLeft <= 0 && light && light.enabled)
light.enabled = false;
From Update().
Thanks for re$$anonymous$$ding me, I need to change the link on my profile to my new page, $$anonymous$$anifest was an old project XD
if(timeLeft <= 0 && light && light.enabled)
{
light.enabled = false;
counting = !counting;
timeLeft = 30;
}
I just added that last bit of code and it works like a charm!! Thank you so much!! :D
Very cool, glad I was able to help you resolve your concern! Happy Developing :D
Your answer

Follow this Question
Related Questions
Inspector doesn't want to show keys in Scripts 2 Answers
Highlight one cell on a GridObject (prefab included) 2 Answers
Use the same input key 1 Answer
Key Press to Trigger animation when within range 1 Answer
How to move an object with a key input? 0 Answers