Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Shankar · Jun 05, 2013 at 10:48 AM · timerealtimetime.time

How to change my stop watch code into real time?

Hi all,

I am developing simple stop watch but i have struck with some problem.

My code is like this:

  using UnityEngine;
  using System.Collections;

  public class Stopwatch : MonoBehaviour {

 public static float minutes;
 public static float seconds;
 public static float fraction;
 public static bool startandstop;
 public GUIText referencetotext;
 public GameObject RefObject;
 
 public static float starttime = Time.time;
 void start() {
     fraction = 0;
      //referencetotext.text="TimerText";
     this.transform.position=RefObject.transform.position;
 }
 void Update() {
     if(startandstop)
     {
     
     if(minutes <= 59)
     {
         if(seconds <=59)
         {
             if(fraction <=100)
             {
                 fraction++;
             }
             else
             {
                 seconds ++;
 
                 fraction = 0;
             }
         }
         else
         {
             minutes++;
             seconds = 0;
                 //seconds -= Time.deltaTime;
         }
     }
     }
         

         

     if(Mathf.Round (starttime) <=0)
            {
                      GameObject.Find("3D").GetComponent<TextMesh>().text=minutes.ToString ("f0") + ":0" + seconds.ToString ("f0")+":0" + fraction.ToString ("f0");
        
     }
     
     else
           {
                     GameObject.Find("3D").GetComponent<TextMesh>().text = minutes.ToString ("f0") + ":" + seconds.ToString ("f0")+":" + fraction.ToString ("f0");
                    
            }
     

     
 }
         
     void OnMouseDown()
     {
     if(this.name == "start_butn")
     {   
         //drag.dragopt=false;
         startandstop = true;
     }
      if(this.name ==  "sec_btn")
         {
          minutes = 0.0f;
         seconds = 0.0f;
         fraction = 0.0f;
         //Application.LoadLevel(0);
         startandstop = false;
         print ("printed");
         //drag.dragopt=false;
         }
 
     
     
      if(this.name == "stop_butn")
     {
         startandstop = false;
         //drag.dragopt=false;
     }
                     
 }
 
     
 }
 

This code is not working for real time. How can i do into realtime.

Thankyou

Shankar

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
1

Answer by Shankar · Jun 06, 2013 at 04:14 AM

  using UnityEngine;
  using System.Collections;
   public class Counterstarted : MonoBehaviour
  {

     private int minutes, seconds,partofSeconds;

     // Use this for initialization
     void Start ()
     {

     }

     // Use this to increment the variables
     void CountDown ()
     {
             partofSeconds++;
             if(partofSeconds == 100) {
             seconds++;                                   // incrementing the seconds
                     partofSeconds = 0;
             }
             if (seconds == 60) {                         // checking if seconds value is 60, if true then incrementing the minutes and resetting the seconds to 0
                     minutes++;
                     seconds = 0;
             }

     }
     //
     void OnGUI ()
     {
             if(GUI.Button(new Rect(10,50,100,50),"start")) {
                     minutes = 0;
                     seconds = 0;
                     partofSeconds = 0;
                     InvokeRepeating ("CountDown", 0, 0.01f);
             }
             if(GUI.Button(new Rect(10,150,100,50),"cancel")) {

                     CancelInvoke ("CountDown");
             }

             GUI.Label (new Rect (10, 10, 150, 30), minutes.ToString ().PadLeft (2, '0') + " : " + seconds.ToString ().PadLeft (2, '0')+ " : " + partofSeconds.ToString ().PadLeft (2, '0'));




This is my code for stopwatch it is perfectly working for real 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 Scribe · Jun 06, 2013 at 02:28 PM 0
Share

Nice, just pointing out that if you pressed the start button several times it will increase the time a lot faster than real time, you should add a boolean to check if the timer is already started.

avatar image
0

Answer by Scribe · Jun 05, 2013 at 12:25 PM

I've changed your code to use the internal time of the game which should be quite accurate:

 using UnityEngine;
 using System.Collections;
  
 public class Stopwatch : MonoBehaviour {
  
 public static float minutes;
 public static float seconds;
 public static float fraction;
 float startTime;
 public static bool startandstop;
 public GUIText referencetotext;
 public GameObject RefObject;
  
 public static float starttime = Time.time;
 void start() {
     fraction = 0;
      //referencetotext.text="TimerText";
     transform.position=RefObject.transform.position;
 }
 void Update() {
     if(startandstop){
         seconds = Mathf.Floor(Time.time - startTime)%60;
         fraction = Mathf.Floor(((Time.time - startTime)-Mathf.Floor(Time.time - startTime))*100);
         minutes = Mathf.Floor((Time.time - startTime)/60);
     }
     GameObject.Find("3D").GetComponent<TextMesh>().text = string.Format("{0:0}:{1:00}:{2:00}", minutes, seconds, fraction);
 }
  
 void OnMouseDown(){
     if(this.name == "start_butn"){   
         //drag.dragopt=false;
         startTime = Time.time;
         startandstop = true;
     }
     if(this.name ==  "sec_btn"){
         minutes = 0.0f;
         seconds = 0.0f;
         fraction = 0.0f;
         //Application.LoadLevel(0);
         startandstop = false;
         print ("printed");
         //drag.dragopt=false;
     }
     if(this.name == "stop_butn"){
         startandstop = false;
         //drag.dragopt=false;
     }
 }
 }

Hope that works for you,

Scribe

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 Shankar · Jun 06, 2013 at 04:12 AM 0
Share

Hey, It has two problems

one is when i click the start button after some time of the play the time is starting from when ever play is started. it will not take for the start button.

another one is when i click reset that is reset to zeros but again i press start button it also not start from zero time.

Any way thanks for the help.

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

15 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I get to display Timetaken to finish the the game scene on the final scene? 1 Answer

need teleport script fixed slightly 1 Answer

Pause Game When Function Is Enabled? 3 Answers

help with movement along x axis and slow down. please help 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