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 manuelmangual · Feb 09, 2017 at 03:07 PM · c#unity 5

How to calculate seconds correctly

In my application I have a countdown timer with a button that decreases the seconds by 30. It works while the seconds are greater than 30 but if its less and I press the button again the seconds give me a negative value and the minutes begin to decrease every second!

Here is my code:

 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 
 public sealed class Timer: MonoBehaviour
 {
     public static float minutes = 0f;
     public static float seconds = 0f;
 
     private bool _stopTime;
     private Text _minuteCounter;
 
     void Start()
     {
         InitializeTime();
     }
 
     public void InitializeTime()
     {
         minutes = 29;
         seconds = 10;
         _minuteCounter = GetComponent<Text>();
         StartCoroutine(WaitForSeconds());
     }
 
     void Stop()
     {
         _stopTime = !_stopTime;
     }
 
     IEnumerator WaitForSeconds()
     {
         while (true)
         {
             yield return new WaitForSeconds(1);
             if (_stopTime) continue;
 
             seconds--;            
 
             if (seconds <= 0)
             {
                seconds = seconds % 60;
                 minutes--;
             }
             GetComponent<Text>().text = string.Format("{0:0}:{1:00}", minutes, seconds);
         }
     }
 }

I know the error occurs in the if clause but I don't know how to fix it. Can anyone help me figure this out? Many thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gjf · Feb 09, 2017 at 03:07 PM

 if (seconds < 0)
 {
     seconds = 59;
     minutes--;
 }
 
 _minuteCounter.text = string.Format("{0:0}:{1:00}", minutes, seconds);

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
0

Answer by MarshallN · Feb 09, 2017 at 03:38 PM

Try putting a Debug.Log in the if( seconds

 if (seconds <= 0)
              {
                 seconds = seconds % 60;
                 Debug.Log(seconds.ToString());
                  minutes--;
              }

and see what kinda output you're actually having.

The % operator in C# can give negative results, check out the reference.

A good way to fix this would be to keep 'seconds' as your only time variable, and have

 displaySeconds= seconds % 60;
 displayMinutes =seconds /60; 
 GetComponent<Text>().text = string.Format("{0:0}:{1:00}", displayMinutes, displaySeconds);

to keep track of minutes and seconds.

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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to define a slider on script file? 1 Answer

How to make buttons have sound when it is highlighted and clicked? 0 Answers

How to call the post function of the class Unity post get check www requests with success callback and error callbacks, 0 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