- Home /
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
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.
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.
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
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
Follow this Question
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