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 T0M · Sep 30, 2015 at 07:56 PM · c#

How to code a simple C# timer?

I think this is quite simple, but I'm fairly new to C sharp so I'm not too sure.

Basically I have a game where the user is a ball, and they have to travel down a long corridor collecting pick-ups a long the way, the game finishes when the player reaches the end of the corridor and they have collected all the pick-ups. What I need help with is coding the timer, I just need a timer that has a format of 00:00:00 (mm:ss:ms) and it starts when the player begins to move. And it stops as soon as all the pickups are collected. There are 7 picks-ups.

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
Best Answer

Answer by $$anonymous$$ · Sep 30, 2015 at 09:00 PM

I don't know how does your code look like but I'll give you a code which displays the time in the format that you'd like to display:

First of all you need 4 variables for this. A variable which represents time, one for milliseconds, one for seconds and one for the minutes. Plus you need another variable to only add up time if you need to.

 private float millisecond, time;
 private int second, minute;
 private bool timerIsOn = true;

In your Update method, the magic happens. First you store Time.deltaTime in your time variable, which is basically the time passed between frames, since your game doesn't always run at 60fps or so. You add up this number, which is around 0.016 per frame or so, as many times as you need, we're gonna use a second here for our measurement. Then it's plain as it is, if a second has passed, you increment your second variable by one, when 60 seconds have passed you increment your minute variable by one etc.. For the milliseconds I used our time variable multipled by 100 to get the correct value.

     void Update () {
         time += Time.deltaTime;
 
         if (timerIsOn) {
             //set second
         //set second
         if (time > 1.0f) {
             second++;
             time = 0.0f;
             //set the minute
             if (second > 59) {
                 second = 0;
                 minute++;
             }
         }
             
             //set millisecond
             millisecond = Mathf.Floor(time * 100.0f);
         }
     }

Then for the displaying. I wrote a method which makes the display nicer, which here means that you don't get any result like having none formatted values like: 1: 1: 5 instead you get 01:01:05. If you want I can explain this to you in comment.

The method looks like this:

 string zero(int _time){
     if (_time < 10)
         return "0";
     else
         return "";
 }

Last but not least you have to display these variable through your OnGUI() method

     void OnGUI(){
         //Draw Time
         GUI.Label (new Rect(Screen.width * 0.025f, Screen.height - Screen.height * 0.2f, 30, 50), zero (minute)+minute.ToString() +":");
         GUI.Label (new Rect(Screen.width * 0.025f + 30, Screen.height - Screen.height * 0.2f, 30, 50), zero (second)+second.ToString() +":");
         GUI.Label (new Rect(Screen.width * 0.025f + 60, Screen.height - Screen.height * 0.2f, 25, 50), zero ((int)millisecond)+millisecond.ToString()); 
     }

To stop the timer when you get 7 objects I'd suggest you to use a code which when your variable which counts the gathered objects turns to 7, basically turn timerIsOn variable to false.

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 T0M · Sep 30, 2015 at 09:16 PM 0
Share

Thanks, with your code I was able to put a working timer that stopped when I collected all 7 objects. I just have another question, I currently have 2 other things on the UI which are Win Text and the Object Count, these are part of the unity UI so I can change the font and colour etc. The timer seems to be separate to that UI, so I was wondering if there was a way to get the timer linked to the unity UI? I can't really explain it very well, but thanks for your help I appreciate it :)

avatar image $$anonymous$$ T0M · Sep 30, 2015 at 09:20 PM 0
Share

I'm not really sure about that. By the way, if you're satisfied with my answer could you please accept it as the proper answer ? :)

avatar image
0

Answer by lloladin · Sep 30, 2015 at 08:27 PM

the simplest way is to Use Time.DeltaTime

example

 Public Float coldown
 public float coldownStore;
 
 public void Start()
 {
 coldownStore = coldown;
 }
 
 public void Update()
 {
 
 if (input.getkey(keycode.Space))
 {
 coldown -= Time.deltatime();
 }
 
 if (coldown <= 0)
 {
 // Do stuff
 coldownStore = coldown;
 }
 }

this basicaly makes the timer go down aslong as you hold space down and if the coldown hits 0 it sets coldown to be = to the coldownStore / resets the timer hope this helps Note this Code is untested and wont work

now i cant help you with the formating right now since i dont have my computer with the code before friday and i cant memorize it :( hope this atleast helps with how to make timers :)

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 T0M · Sep 30, 2015 at 09:01 PM 0
Share

Thanks that really helps :)

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

29 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

Related Questions

Trying to get Player to shoot in both directions depending on what side he is facing? 0 Answers

Loot table problems 1 Answer

Left hand side of an assignment must be a variable, a property or an indexer 1 Answer

How would i modify my current script to remove these UI buttons without killing my pause menu? 1 Answer

How can I instantiate a object on the world coordinates 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