Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-2
Question by MH1 · Jun 05, 2013 at 01:49 AM · c#javascriptvariablecountdown

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.)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!");
     }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Memige · Jun 07, 2013 at 08:44 PM 1
Share

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.

avatar image MH1 · Jul 15, 2013 at 01:08 PM 0
Share
 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;
 }
 }
avatar image Memige · Jul 15, 2013 at 04:07 PM 1
Share

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.

avatar image MH1 · Jul 16, 2013 at 07:36 PM 0
Share

Your right. I didn't see that answer at first.

avatar image
6

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());
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Dave-Carlile · Jun 05, 2013 at 01:51 AM

 for (int i = 10; i >= 0; i--)
 {
   
 }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image numberkruncher · Jun 05, 2013 at 01:56 AM 2
Share

I think you meant for (int i = 10; i >= 0; --i) Your loop will otherwise go forever! ;)

avatar image numberkruncher · Jun 05, 2013 at 01:58 AM 1
Share

@$$anonymous$$H1:

C#:

 for (int i = 10; i >= 0; --i) {
 }

UnityScript:

 for (var i = 10; i >= 0; --i) {
 }
avatar image Dave-Carlile · Jun 05, 2013 at 02:41 AM 2
Share

@numberkruncher oops - you are correct. It's such a habit to type in the ++ :)

avatar image Dave-Carlile · Jun 05, 2013 at 12:40 PM 1
Share

You're not getting any log messages? Is this script attached to a game object?

avatar image Dave-Carlile · Jun 05, 2013 at 09:54 PM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GetComponent, What is it ? 1 Answer

How to Typecast JS Variables as C# Classes? 0 Answers

Can't access a javascript static variable from c# script 1 Answer

Javascript and C#, different behaviour in inspector ? 1 Answer

Need to use 2 different language scripts. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges