Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
3 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 schwertfisch · Dec 09, 2010 at 09:31 PM · timerseconds

Timer's tenths of seconds visible digits issue

I use a simple script for displaying the tenths of seconds on a timer. The following script is called GUITimerFragment.js and is applied to an empty game object I call GUI.

var startTime;

function Awake () {

 startTime = Time.time; 

}

function OnGUI () {

var runningTime = ((Time.time-startTime)*10)%10;

runningTime = String.Format ("{0:0}", runningTime);

GUI.Label(Rect(83,323,200,200), runningTime.ToString());

}

The issue is this: the timer begins counting the tenths of seconds normally: 1...2...3...4...5...6...7...8...9...10 only that I want it to display 0 instead of 10. I thought that the line

String.Format ("{0:0}", runningTime)

would make it show only one digit but it seems I'm wrong.

What I see is this (vimeo video)

What do I have to do to make it show "0" instead of "10"? - thanks :-)

Comment
Add comment · Show 1
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 Jesus_Freak · Dec 09, 2010 at 10:00 PM 0
Share

you could do if(runningTime == 10) {runningTime = 0} to reset it to 0 everytime it 'should' be 10.

3 Replies

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

Answer by Peter G · Dec 09, 2010 at 10:15 PM

See this page. Unity has a built-in function for working with modulo and floats

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 schwertfisch · Dec 11, 2010 at 11:28 PM 0
Share

Thanks so much. Didn't know this function. I ended up solving my problem this way.

avatar image Leon700 · Feb 07, 2018 at 01:40 PM 0
Share

https://docs.unity3d.com/ScriptReference/$$anonymous$$athf.Repeat.html

avatar image
1

Answer by Statement · Dec 09, 2010 at 09:52 PM

It seems there was some strange thing going on where you were using the mod operator on a float. I cast this to an integer to floor the value.

Also String.Format ("{0:0}", runningTime); means to return runningTime without any decimals. It doesn't constrain the number to one digit.

var startTime : float;

function Awake () { startTime = Time.time; }

function OnGUI () { var runningTime : int = ((Time.time - startTime) * 10) % 10; var runningTimeText : String = String.Format ("{0:0}", runningTime); GUI.Label(Rect(83,323,200,200), runningTimeText); }

Here is another way of achieving the same result:

import System;

var startTime : float; var rect = Rect ( 83, 323, 200, 200 );

function Awake () { startTime = Time.time; }

function OnGUI () { var span = TimeSpan.FromSeconds(Time.time - startTime); var tenths = span.Milliseconds / 100;

 GUI.Label ( rect, tenths.ToString() ); 

}

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 schwertfisch · Dec 11, 2010 at 11:31 PM 0
Share

Thanks for your answer. The float was indeed a problem. You helped me make things more clear. In the second script though, what is the role of "import System"? Excuse me if this is a total noob query, but I don't know where to look it up.

avatar image Statement · Dec 12, 2010 at 12:20 AM 0
Share

import System; reduces the namespace System to global namespace. It is there because Time and TimeSpan are types that reside in the "System" namespace. Look up "namespace" in wikipedia. Without import System; we have to type in the full name to the types, such as: System.Time and System.TimeSpan. It is the same as using System; in C#.

avatar image
1

Answer by yoyo · Dec 09, 2010 at 11:36 PM

For formatting elapsed time for GUI display I use this method ... easily tweakable if you want tenths, or hours or other variations.

public string FormatTime(float time)
{
    int d = (int)(time * 100.0f);
    int minutes = d / (60 * 100);
    int seconds = (d % (60 * 100)) / 100;
    int hundredths = d % 100;
    return String.Format("{0:00}:{1:00}.{2:00}", minutes, seconds, hundredths);
}
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 schwertfisch · Dec 11, 2010 at 11:31 PM 0
Share

Thank you yoyo, very useful. ;-)

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

1 Person is following this question.

avatar image

Related Questions

Referencing other scripts: -5 seconds rule on timer 1 Answer

How to make a timer? 1 Answer

CountDown Timer Help (Seconds problem) 2 Answers

My high time score isn't working 1 Answer

why the debree timer float not work 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