x


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

more ▼

asked Jun 05 '13 at 10:48 AM

Shankar gravatar image

Shankar
98 6 20 33

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
 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.

more ▼

answered Jun 06 '13 at 04:14 AM

Shankar gravatar image

Shankar
98 6 20 33

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.

Jun 06 '13 at 02:28 PM Scribe
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jun 05 '13 at 12:25 PM

Scribe gravatar image

Scribe
2.9k 13 17 36

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.

Jun 06 '13 at 04:12 AM Shankar
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x7796
x869
x793
x77
x42

asked: Jun 05 '13 at 10:48 AM

Seen: 816 times

Last Updated: Jun 06 '13 at 02:28 PM