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 /
avatar image
0
Question by MrAwesome343 · May 30, 2016 at 03:13 AM · timetimer countdown

Making a simple countdown timer

I'm working on a simple racing game and I want to add a countdown timer, but I can't get it to work. Any advice after seeing the code?

 public float countdown;
 
     void Start()
     {
         Time.timeScale = 0f;
     }
 
     void Update()
     {
         countdown -= Time.deltaTime;
         if (countdown < 0)
         {
             Time.timeScale = 1f;
         }
         else if (countdown > 0)
         {
             Time.timeScale = 0f;
         }
     }
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

1 Reply

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

Answer by DeathDev · May 30, 2016 at 03:31 AM

Since you initially set Time.timeScale to 0, Time.deltaTime is always going to be 0.

(note - Time.timeScale changes the timeScale for everything in the current scene, not a single object.)

Here's a small script I created for a countdown:

 using UnityEngine;
 using System.Collections;
 
 public class Countdown : MonoBehaviour {
 
     public float countdown = 10.0f; //The amount of time to wait
     private float _time = 0;        //The current progress
     private bool called = false;    //If the countdown has been met.
 
     void Update()
     {
         if (!called)
         {
             _time += Time.deltaTime;
             if (_time >= countdown)
             {
                 called = true;
                 DoStuff();
             }
         }
     }
 
     void DoStuff()
     {
 
     }
 
 }

I did it this way because of the nature of your question. Due to your statement about how it is a racing game, I assumed you would want an onscreen countdown of sorts, of which this script supports. If you did not want to do an OnScreen countdown, then you could simply do:

 public class Countdown : MonoBehaviour {
 
     public float countdown = 10.0f; //The amount of time to wait
 
     void Start()
     {
         Invoke("DoStuff", countdown);
     }
 
     void DoStuff()
     {
 
     }
 
 }
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 MrAwesome343 · May 31, 2016 at 02:09 AM 0
Share

Okay, the way I have it set works, but how can I make it count down ins$$anonymous$$d? I want to have the text displayed on the screen

avatar image MrAwesome343 MrAwesome343 · May 31, 2016 at 02:13 AM 0
Share

Never $$anonymous$$d, I fixed it, I must have missed something when I tried it first, thank you!

avatar image DeathDev MrAwesome343 · May 31, 2016 at 02:32 AM 0
Share

Great! Sorry about the late reply, I honestly forgot about this answer for a bit. If you believe that what I provided was the correct answer be sure to mark it! :p

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

44 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

Related Questions

Farming game Time management 0 Answers

Clock doesn't stop when reaches 0.0. Can you help me with this. 1 Answer

How to create a 30 min demo scene? 0 Answers

How can I make this timer become longer over time? (When I click it memorizes then finishes it increases wait time) 0 Answers

Allow player to Press Input.GetKeyDown once per update ? 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