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

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.

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