Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by DissonanceMask · Sep 27, 2019 at 11:09 PM · timer countdownwhile-loop

Why is my Timer like this?

I know my code may be messy cause im still a beginner, gimme a break. I dont understand how the do while loop worked this way. I wanted it to count down from 2 to 0, as assigned in "countDownTime". If i made it while(countDownTime >= 0), it would actually start the countdown from 0. I dont understand the logic of it not counting down from 2, when I assigned it to countdown while (countDownTime > = 0), and it wouldnt even stop at 0,; it would keep going negative.

using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.UI; using TMPro; public class DuelGame : MonoBehaviour { [SerializeField] TextMeshProUGUI p1ScoreBox; [SerializeField] TextMeshProUGUI p2ScoreBox; [SerializeField] TextMeshProUGUI timer1; [SerializeField] TextMeshProUGUI timer2; [SerializeField] float countDownTime = 2f; float time; bool hasStarted = false; bool timerStart = false; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Space) && !hasStarted) { hasStarted = true; StartGame(); } DisplayTime(); } void DisplayTime() { if (timerStart) { do { countDownTime -= Time.deltaTime; } while (countDownTime >= 2); timer2.text = Math.Round(countDownTime, 2).ToString(); } }

Comment
Add comment · Show 1
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 Mouton · Sep 27, 2019 at 11:55 PM 0
Share

You need to add 4 spaces before every lines in order to get the code formated

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ErikH2000 · Sep 27, 2019 at 11:57 PM

You really should figure out the code formatting so your code doesn't jumble together in one line.

Your do...while loop is always going to subtract some amount of time from your countdown. You could try a while loop instead, which checks the expression at the beginning of the loop (instead of the end, like you have it). If the expression isn't true, then it won't enter the loop and subtract from your countdown.

 while (countDownTime >= 2) {
   countDownTime -= Time.deltaTime;
 }

There is another way to check for time that I like to use, because you don't need to update variables:

  public class DuelGame : MonoBehaviour { 
      const float COUNTDOWN_DURATION = 2f;
      float countdownStartTime;
      bool hasStarted = false;

      void Update() { 
          if (Input.GetKeyDown(KeyCode.Space) && !hasStarted) {
              hasStarted = true;
              countdownStartTime = Time.time;
              StartGame(); 
          }
          DisplayTime();
      } 

      void DisplayTime() { 
          if (hasStarted) { 
              float elapsedSinceStarted = Time.time - countdownStartTime; 
              if (elapsedSinceStarted <= COUNTDOWN_DURATION) {
                  timer2.text = Math.Round(COUNTDOWN_DURATION - elapsedSinceStarted, 2).ToString(); 
              } 
          }
      }
 }
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

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

112 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you make a time bomb beeper 0 Answers

[Closed]Unity crash, while/for loop 3 Answers

Can I use a while() in the function OnDrawGizmos() ? 1 Answer

While loop crashes unity 1 Answer

How to have a Count Down Timer carry over to a new level? 2 Answers


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