Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Flaymes · Oct 27, 2020 at 02:06 PM · countdowntimer-script

Countdown Timer

I am trying to make a game where you have 3 seconds to shoot a moving alien that keeps respawning. I want it to be that when I shoot it it sets the time back to 3 seconds so I have 3 seconds to try and shoot it again and e.t.c until I dont manage to shoot it in 3 seconds and I lose. I have everything in place except the timer script that I want to countdown showing seconds and milliseconds. Could you please try to make this script countdown from a number instead of up from 0?

using UnityEngine; using UnityEngine.UI; using TMPro;

public class Timer : MonoBehaviour { public TextMeshProUGUI timerText; public bool playing; public float timer;

 void Update()
 {
     if (playing == true)
     {
         timer += Time.deltaTime;
         UpdateText(timer, timerText);
     }
 }

 void UpdateText(float t, TextMeshProUGUI text)
 {
     int seconds =- Mathf.FloorToInt(t % 60f);
     int milliseconds =- Mathf.FloorToInt((t * 100f) % 100f);
     text.text = seconds.ToString("00") + "." + milliseconds.ToString("0");
 }

}

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 Duckocide · Oct 27, 2020 at 03:08 PM

I'd use a coroutine for this. Have a property (boolean) to indicate if times up or not. Every time you want to start the 3 second timer, call the public method "StartTimer" - It will safely stop and start a new timer. In your Update method, monitor the timesUp property ... If it goes true, time is up!

 public bool timesUp = false;
 
 public void StartTimer() 
 {
     StopCoroutine(StopWatchCR());
     StartCoroutine(StopWatchCR());
 }
 
 private IEnumerator StopWatchCR()
 {
     timesUp = false;
     yield return new WaitForSeconds(3);
     timesUp = true;
 }
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 Flaymes · Oct 27, 2020 at 03:14 PM 0
Share

Its fine Duckocide I just spent the whole day trying to figure this out until i realised i dont need the whole complicated mess of milliseconds and seconds. I literally just need to say the timer is -= to time.deltatime and then put "0.00" into the .ToString() brackets.

avatar image Duckocide Flaymes · Oct 27, 2020 at 04:08 PM 0
Share

No worries. Just so you know, A coroutine is a great way to do something in parallel to the core unity lifecycle of Update(), FixedUpdate(), LateUpdate() etc... They can often improve performance as your not slowing Unity down.

avatar image
0

Answer by johnburkert · Oct 27, 2020 at 05:16 PM

You can replace the mess inside of UpdateText with a call to ToString

 timerText.text = timer.ToString("F"))

https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

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

138 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

Related Questions

Make a countdown timer say "go" once it reaches 0 1 Answer

How to set count-down timer for a muli-player game in C# UNET? The timer must only start when all the players are connected. 1 Answer

How to subtract 1 from an int each second? 2 Answers

unity editor stck if i give a value greater than 0 to my time variable.whats the problem? 1 Answer

Why does my MatchTime Class not work when using Properties? 1 Answer


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