• 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
22
Question by Michael 8 · Mar 09, 2012 at 12:37 AM · c#guitimercountercountdown

C# countdown timer

I'm trying to create a c# countdown timer for my level which should also be displayed in the GUI. The player needs to destroy certain items to get extra time. If the player runs out of time it's game over. It the player destroyed all the items they are presented with the time left and then can play again to see if they can finish the level faster with more time remaining.

I'm not sure what the script is to initiate a timer and then count down with seconds. The initial time should obviously be a public parameter you can tweak from inside the editor.

Running out of time should run an if statement to initiate a game over screen for losing. Destroying all the objects should stop the timer and record the remaining time to display in the winning game over screen.

Destroying certain objects will add time to the timer in seconds or fractions of seconds.

So I know what I want to do, but the Syntax is giving me nightmares. :-O Any help will be appreciated.

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

9 Replies

· Add your reply
  • Sort: 
avatar image
64

Answer by rutter · Mar 09, 2012 at 01:06 AM

You could create a script that keeps track of how much time is left.

That object's update loop can subtract Time.deltaTime from that value, and check if it's below zero:

var timeLeft = 30;

 function Update()
 {
     timeLeft -= Time.deltaTime;
     if ( timeLeft < 0 )
     {
         GameOver();
     }
 }

Other objects could access that value, to display it in the GUI or add to it as needed.

Comment
Add comment · Show 7 · 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 DESTRUKTORR · Jul 07, 2012 at 06:46 PM 5
Share

That's definitely Javascript. The asker requested C#.

avatar image HarryGodden · Oct 13, 2013 at 06:59 PM 18
Share

Translated to C# it would be

 float timeLeft = 30.0f;
     
     void Update()
     {
         timeLeft -= Time.deltaTime;
         if(timeLeft < 0)
         {
             GameOver();
         }
     }
 


-Thanks to Stone Legion for telling me that timeLeft is meant to be a float.

avatar image enoy HarryGodden · Aug 29, 2017 at 08:55 PM 0
Share

It is easier than I thought, good rutter.

avatar image Stone-Legion · Oct 22, 2013 at 04:36 AM 1
Share

int timeLeft = 30; should be a float, not int as Time.deltaTime returns a float.

avatar image QQQ_QQQ · Oct 21, 2019 at 09:00 AM 0
Share

Hello, I know it should work but apparently i do something wrong. Wrote your code in Update function but it is not working. When i set "float timeLeft = 30.0f;" it is always like 29,98108 and changing only 3 last digits.

avatar image HarryGodden QQQ_QQQ · Oct 21, 2019 at 10:16 PM 0
Share

$$anonymous$$ake sure you are defining timeLeft=30.0f outside of the update function !! Sounds like this is the issue.

avatar image CodyBlack · May 10, 2020 at 06:43 PM 0
Share
 public float blablabla = 10 ;
 
   void Update()
      {
          blablabla -= Time.deltaTime;
          if( blablabla < 0) {print ("Rutter cool"); }
      }


avatar image
33

Answer by U_Ku_Shu · Nov 25, 2016 at 09:57 AM

 float currCountdownValue;
 public IEnumerator StartCountdown(float countdownValue = 10)
 {
     currCountdownValue = countdownValue;
     while (currCountdownValue > 0)
     {
         Debug.Log("Countdown: " + currCountdownValue);
         yield return new WaitForSeconds(1.0f);
         currCountdownValue--;
     }
 }


To start you need to call:

 StartCoroutine(StartCountdown());

Comment
Add comment · Show 5 · 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 halfadozen · Sep 11, 2017 at 04:58 PM 0
Share

Excellent.

avatar image StellarVeil · Sep 15, 2018 at 08:29 PM 0
Share

Yeah I think Coroutine is the best way for this stuff if you want to make it separate and more organised.

avatar image Fressno · Sep 15, 2019 at 02:42 AM 0
Share

how would i go by and make the countDownValue public in the inspector? or even accessable with other scripts?

avatar image unity_Q0hIdQ-QWZIYQA Fressno · Apr 30, 2021 at 04:21 PM 1
Share

You would use public float currCountdownValue; in line 1.

avatar image timothywwcase Fressno · May 01, 2021 at 05:21 PM 0
Share

As noted in the reply above you'd just have to mark it as public, or if you wanted it accessible in the Inspector but didn't need to access it from other scripts you could use [SerializeField] float currCountdownValue; .

avatar image
5

Answer by krister.collin · Mar 09, 2012 at 07:43 AM

Michael,

That's quite a request, but I like a challenge, so here we go.

Firstly, the structure that probably best suits this is: - Game Controller (in charge of game win/loss and messaging) - Timer (GuiText object that displays the time left on screen. It is VERY important that Timer is a child of the Game Controller gameobject) - Items (the onscreen, destructable items you mentioned. These must also be child objects of the Game Controller)

The Game Controller would look something like this:

 using UnityEngine;
 using System.Collections;
 
 public float timeIncrease = 2;
 public bool timeElapsed = false;
 
 public int items;
 
 void Start()
 {
     //Gather how many items are remaining
     GameObject[] items = GameObject.FindObjectsWithTag("items") as GameObject[];
     itemsRemaining = items.length;
 
     //The timer, as a child of this gameobject, receive this and start the countdown using the timeRemaining variable
     BroadcastMessage("Start Timer", timeRemaining);
 }
 
 void Update()
 {
     
     if (itemsRemaining == 0)
     {
         //You win!
     }
 
     if (timeElapsed)
     {
         //You lose!
     }
 }
 
 //If the game controller receives this signal from the timer, it will end the game
 void timeHasElapsed()
 {
     timeElapsed = true;
 }
 
 //If the Game Controller receives this signal from a destroyed item,
 //  it sends a message to the time object to increase the time left
 void itemDestroyed()
 {
     increaseTime();
     
 }
 
 void increaseTime()
 {
     broadcastMessage("timeIncrease", timeIncrease)
 }
 
 
 
 //I've included this dead function because I can't test the code myself right now and I don't want to leave
 // you with errors. IT may or may not be needed, though.
 void timeIncrease
 {}

The purpose of the Game Controller would be to receive the happenings of both the items and timers and pass the appropriate responses back and forth, as well as handle the win/lose condition.

Then, you would have this script attached to your timer object:

 using UnityEngine;
 using System.Collections;
 
 public float timeRemaining = 60f;
 
 void Start()
 {
     InvokeRepeating("decreaseTimeRemaining", 1.0, 1.0)
 }
 
 void Update()
 {
     if (timeRemaining == 0)
     {
         sendMessageUpward("timeElapsed");
     }
 
     GuiText.text = timeRemaining + " Seconds remaining!";
 
 }
 
 void decreaseTimeRemaining()
 {
     timeRemaining --;
 }
 
 //may not be needed, left it in there
 void timeElapsed()
 {}


And, in the script for your onscreen items, you would include this:

 void OnDestroy()
 {
     SendMessageUpwards("itemDestroyed")
 }
 
 void itemDestroyed()
 {}



I have to apologise in advance, though, I have no ability to test this code right now and I had to crank it out quickly, so it will have errors, but remember, Game Controller is the parent to the timer and items, as they will have to send messages back and forth and, in this code, the game controller will handle that.

Comment
Add comment · Show 1 · 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 thef1chesser · Oct 29, 2013 at 10:07 AM 0
Share

this seems pretty good if you ask me. The only thing that I personally would have done differently to control time more is use System.Diagnostics.Stopwatch.

This way you can start the timer when you want to and the time displayed in the GUI is (int)timeRemaining - Stopwatch.Elapsed.Seconds

avatar image
5

Answer by Denscrivent · Jun 17, 2015 at 10:01 AM

I have figured out how to make a countdown timer.

 public class StartCountdown : MonoBehaviour {
     int time,a;
     float x;
     public bool count;
     public string timeDisp;
 
     void Start () {
         time = 3;
         count = false;
     }
     
     // Update is called once per frame
     void FixedUpdate (){
         if (count){
             timeDisp = time.ToString ();
             GameObject.Find ("StartCounter").GetComponent<Text> ().text = timeDisp;
             x += Time.deltaTime;
             a = (int)x;
             print (a);
             switch(a){
                 case 0: GameObject.Find ("StartCounter").GetComponent<Text> ().text = "3"; break;
                 case 1: GameObject.Find ("StartCounter").GetComponent<Text> ().text = "2"; break;
                 case 2: GameObject.Find ("StartCounter").GetComponent<Text> ().text = "1"; break;
                 case 3: GameObject.Find ("StartCounter").GetComponent<Text> ().enabled = false;
                     count = false; break;
             }
         }
     }
 }
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 walkeraki · Jul 06, 2017 at 01:25 PM 0
Share

$$anonymous$$ore easy way with a lot less redundent stuff that you have.

if (count) { GameObject.Find ("StartCounter").GetComponent ().text = ""+timeDisp; x += Time.deltaTime; a = (int)x; GameObject.Find ("StartCounter").GetComponent ().text = ""+a; if(a==0) count=false; }

avatar image mlglock · Apr 17, 2018 at 03:11 PM 0
Share

Hard to follow your code if you name variables like this.

avatar image
3

Answer by MyrDovg · Oct 30, 2017 at 11:31 AM

         float totalTime = 120f; //2 minutes
 
         private void Update()
         {
             totalTime -= Time.deltaTime;
             UpdateLevelTimer(totalTime );
         }
 
         public void UpdateLevelTimer(float totalSeconds)
         {
             int minutes = Mathf.FloorToInt(totalSeconds / 60f);
             int seconds = Mathf.RoundToInt(totalSeconds % 60f);
 
             string formatedSeconds = seconds.ToString();
 
             if (seconds == 60)
             {
                 seconds = 0;
                 minutes += 1;
             }
 
             timer.text = minutes.ToString("00") + ":" + seconds.ToString("00");
         }
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 Jasmin1347 · Mar 28, 2018 at 03:00 AM 0
Share

Can you explain this code?

avatar image luckynamal · Aug 08, 2018 at 12:50 PM 0
Share

Awesome. Thanks Dude. Stay Blessed

  • 1
  • 2
  • ›

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

35 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

Related Questions

Problems in c# countdown timer 1 Answer

How to stop a timer with GUI button? 1 Answer

How to add a counter on screen! 2 Answers

Countdown timer into text c# 1 Answer

Coin Counter still not working after 5+ hours of frustrating changing code and moving stuff around 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