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 /
  • Help Room /
This question was closed Aug 31, 2016 at 11:14 PM by yeoldwarchap.
avatar image
0
Question by yeoldwarchap · Jul 21, 2016 at 07:13 PM · c#unity 5timercountdowntimer countdown

Countdown timer...

In a game I'm making I want to have a countdown timer that starts at 1:00 and goes down to 0:00. I want the timer to be displayed in some text that I have placed down. I have tried for a long time to get this to work but it just won't. Please help me.

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

  • Sort: 
avatar image
1

Answer by Charmind · Jul 21, 2016 at 08:39 PM

Simple as this:

 // Remember using using UnityEngine.UI;
 
 public Text text; // set it to your timer text on GUI
 
 float tim = 60;
 
 void Update()
 {
 text.text = (Mathf.Floor(tim/(60*60)))+":"+(Mathf.Floor(tim/60)%60)+":"+Mathf.Floor(tim%60);
 
 tim -= Time.deltaTime;
 }


Explanation:

1st thing in "text =" is hour counter, next is minute counter modulo 60 so it will display 0-59 minutes and not more because that goes to hour section. Next is seconds modulo 60 because 60 seconds is minute so it displays 0-59 seconds on it.

just in case you dont know what modulo is:

it's symbol is "%" and it returns rest from division, so 20%21 = 20 and 21%21 = 0 22%21 = 1 and so on. just loop of numbers with given lenght.

Comment
Add comment · Show 2 · 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 yeoldwarchap · Jul 23, 2016 at 03:52 PM 0
Share

I'm trying to make it so that when I press the Q button, the timer starts but for some reason it won't. I had the timer working before I stopped it running when the game started... Here's the scripts I'm using.

public class TimeCounter : $$anonymous$$onoBehaviour {

 bool startCounter;
 

 public Text text; // set it to your timer text on GUI

 float tim = 60;

 void Start()
 {
     startCounter = false;
 }

 public void StartTimeCounter()
 {
     startCounter = true;
 }

 public void StopTimeCounter()
 {
     startCounter = false;
 }

 void Update()
 {
     if (startCounter)
     {
         print("hmmmm");
         text.text = ($$anonymous$$athf.Floor(tim / 60) % 60) + ":" + $$anonymous$$athf.Floor(tim % 60);

         tim -= Time.deltaTime;
     }
 }

}

and this is in another script:

void Update () { if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Escape)) { Application.Quit(); }

     //if (scoreUITextGO.GetComponent<GameScore>().Score > 0);
     if (Input.Get$$anonymous$$eyDown("q"))
     {
         print("yay");
         TimeCounterGO.GetComponent<TimeCounter>().StartTimeCounter();
     }

Thanks

avatar image yeoldwarchap · Jul 25, 2016 at 02:04 PM 0
Share

In StartTimeCounter() you should reset tim to 60

avatar image
0

Answer by commodore · Jul 21, 2016 at 08:54 PM

 private float timeInSeconds = 60;

 void Update() {
     float minutes = Mathf.FloorToInt (timeInSeconds / 60);
     float seconds = Mathf.FloorToInt (timeInSeconds) % 60;

     Debug.Log (string.Format("{0}:{1}", minutes.ToString("0"), seconds.ToString("00")));

     timeInSeconds -= Time.deltaTime;
 }
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

Follow this Question

Answers Answers and Comments

221 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 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

Project Spark to C# - Started To, While, and No Longer 1 Answer

Traffic Light won't change colors State machine 0 Answers

Need to create a Time Trial in Unity with C# 1 Answer

How to get a countdown timer with values entered by the player 0 Answers

Player Countdown Interaction/Time Reduction+Addition 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