- Home /
Make a variable start at 10 and count down until 0
I want to make it so that when a key is pressed a variable starts counting down from 10 to 0. C# or Java Script is fine. (I looked in other questions similar to this, but couldn't find what I was looking for.)
Answer by MH1 · Jun 07, 2013 at 08:41 PM
In the end, this is what worked. C#:
if (Input.GetKeyDown("a"))
{
countdown();
StartCoroutine(countdown());
}
IEnumerator countdown()
{
yield return new WaitForSeconds(10.0f);
Debug.Log ("It's been 10 seconds!");
}
This doesn't actually answer the question you asked. I'm glad it worked for your case, but what this does is simply wait ten seconds, it has no affect on variables at all. $$anonymous$$i$$anonymous$$o51's answer is correct for the actual question asked.
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour {
private float cd = 10;
IEnumerator countdown()
{
yield return new WaitForSeconds(1.0f);
cd-=1;
yield return new WaitForSeconds(1.0f);
cd-=1;
yield return new WaitForSeconds(1.0f);
cd-=1;
yield return new WaitForSeconds(1.0f);
cd-=1;
yield return new WaitForSeconds(1.0f);
cd-=1;
yield return new WaitForSeconds(1.0f);
cd-=1;
yield return new WaitForSeconds(1.0f);
cd-=1;
yield return new WaitForSeconds(1.0f);
cd-=1;
yield return new WaitForSeconds(1.0f);
cd-=1;
yield return new WaitForSeconds(1.0f);
cd-=1;
}
}
You are certainly making progress; however, there is a reason 5 people has taken the time to upvote $$anonymous$$i$$anonymous$$o51's answer. The latest code you have posted above in fact does the same thing as $$anonymous$$iko's, with one crucial difference: $$anonymous$$i$$anonymous$$o's solution is much friendlier to look at and easily adjusted. The compiler is going to see the two code chunks as nearly identical, but if you wanted to adjust the timer to be a different value using your method you would need to copy paste or delete yield return new WaitForSeconds(1.0f); cd-=1;
pairs for each increment or decrement. Using $$anonymous$$i$$anonymous$$o's solution, all you would need do is change the default value of the countdownValue float declared at the top of the file. This is much cleaner and less prone to copy pasta errors. I'm self aware enough to realize this may come across as elitist to a growing programmer, but please take it from experience, its best to get into solid program$$anonymous$$g practices early. $$anonymous$$eeping your code as stable, flexible, and elegant as possible will do nothing but benefit you in the long run. One good lesson on this one is anytime you see yourself pasting (or typing out) the same lines of code more than twice, you probably have an opportunity to condense it into a loop.
Answer by MiKo51 · Jun 06, 2013 at 02:22 AM
float countdownValue=10;
float countdown;
public Ienumerator StartCountdown()
{
countdown = countdownValue;
while (countdown >0)
{
yield return new WaitForSeconds(1.0f);
countdown --;
}
}
StartCoroutine(StartCountdown());
Answer by Dave-Carlile · Jun 05, 2013 at 01:51 AM
for (int i = 10; i >= 0; i--)
{
}
I think you meant for (int i = 10; i >= 0; --i)
Your loop will otherwise go forever! ;)
@$$anonymous$$H1:
C#:
for (int i = 10; i >= 0; --i) {
}
UnityScript:
for (var i = 10; i >= 0; --i) {
}
@numberkruncher oops - you are correct. It's such a habit to type in the ++ :)
You're not getting any log messages? Is this script attached to a game object?
Well, that wasn't really your question. Are you wanting to just wait 10 seconds and then do something? For that you can use the Invoke function and pass in a wait time.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103540im_/https://answers.unity.com/themes/thub/images/avi.jpg)