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 Brosilio_Gaming · Aug 31, 2015 at 10:29 AM · time

How to make a timer that counts up in seconds, as an int?

Im working on a simple game that relies on your total time in SECONDS ONLY to do some stuff. but Time.time gives me the value, but as a float. i just need a way to get it as an int. or a way to convert it. heres what i have so far:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 
 public class UserScoreTracker : MonoBehaviour {
 
 
 
     //the amount of time it took to finish the level.  its the TIME, but its called score. 
     public int score = 0; // <-- NEEDS TO BE INT!!!!!
     private bool Aktive; //heh lol "Aktive" XD
     //the username input box/and where to store it and also waht to do with it.
     public string username;
     public GameObject ScoreUI;
     public void UserInput(InputField userField)
     {
         username = userField.text;
     }
 
     public void Update()
     {
         score = Time.time;
     }
 
     public void SubmitSkcoar()
     {
         Highscores.AddNewHighscore(username,score);
     }
     public void OnTriggerEnter()
     {
         ScoreUI.SetActive(true);
     }
 }
 

Can someone 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

5 Replies

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

Answer by darthtelle · Aug 31, 2015 at 10:52 AM

You still want the timer to count as a float, because that is what time is. However, when you get the value out of your score (time) float you can convert it into int seconds.

 float timer = 0.0f;
 
 void Update()
 {
    timer += Time.deltaTime;
    int seconds = timer % 60;
 }
Comment
Add comment · Show 6 · 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 Brosilio_Gaming · Aug 31, 2015 at 02:51 PM 0
Share

Thanks for the help, but it still says it cant convert "type int with type float" but i tested, and it does give a whole value, but it isnt actually an integer; unity still stores it as a float, otherwise it throws errors. The reason i need this to work is because im making a scoreboard that is based of $$anonymous$$imal time in a race, but the scoreboard wont display float values. ive tried everything, but i just ended up just using whole values. :P

avatar image darthtelle · Aug 31, 2015 at 02:59 PM 0
Share

@Brosilio_Ga$$anonymous$$g No problem. We all have to start somewhere! $$anonymous$$ake sure to keep on track with maths and watch the Unity Live Training videos. They are tee$$anonymous$$g with useful tidbits!

avatar image Brosilio_Gaming · Aug 31, 2015 at 03:31 PM 0
Share

@darthtelle thats true. Thanks for the help anyway!

avatar image morphman_mc · May 14, 2018 at 11:47 AM 0
Share

thx, i used it to know for how long to jump

avatar image erw29 · Jul 03, 2020 at 12:59 PM 0
Share

Is there a delta time drift in Unity?

Show more comments
avatar image
5

Answer by rm-square · Mar 16, 2020 at 08:46 PM

As people come and read, i like to add another solution for a counter. The advantage in this solution is, that the Coroutine doesn't run in the Update method and won' be called every single frame.

This code will increase your score(time needed) variable every second by 1.

 public int score = 0;
 
 public void Start(){
     StartCoroutine(time());
 }

 IEnumerator time(){
     while (true)
     {
         timeCount();
         yield return new WaitForSeconds(1);
     }
 }

 void timeCount(){
     score += 1;
 }

When the level is finished and you put your score in the scoreboard, you should stop the counter with

 StopCoroutine(time());
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 erw29 · Jul 03, 2020 at 03:11 PM 0
Share

Nope, that one is depending on the fps. It might work for seconds, but not for milliseconds.

avatar image
3

Answer by Content1of_akind · Apr 14, 2019 at 01:57 AM

if your C# compiler doesn't allow ---> seconds = Convert.ToInt32( timer % 60) or ---> int seconds = timer % 60; which mine does not for some reason, you can use this code instead to convert a float to an int. I'm using Visual Studio 2017 C# compiler

     public float timer = 0.0f;
     public int seconds;
 
     void Update()
     {
         // seconds in float
         timer += Time.deltaTime;

         // turn seconds in float to int
         seconds = (int)(timer % 60);
         print(seconds);
     }
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
1

Answer by Cibum · Apr 19, 2020 at 10:57 AM

Why not just

 void Update()
 {
     timer = Time.time;
 }

?

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 ltomov · Feb 18, 2021 at 04:15 PM 1
Share

The documentation doesn't recommend it: "Regular (per frame) calls should be avoided: Time.time is intended to supply the length of time the application has been running for, and not the time per frame."

avatar image
0

Answer by Stefan93 · Aug 19, 2017 at 08:26 AM

Now the code is complete...

float timer = 0.0f; int seconds ;

void Update() { timer += Time.deltaTime; seconds = Convert.ToInt32( timer % 60); // you can use the seconds wherever you wish }

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

38 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

Related Questions

Resetting scene time after 13 seconds 1 Answer

I make a timing score script in unity 2D, how i make high score of time?? Script is as follow: 1 Answer

Add x amount in t seconds? 2 Answers

Get position of object that uses Perlin Noise X distance/time back/offset? 0 Answers

Change scene after event (time or clicking on object) 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