ada dudu ada dudu - 3 months ago 9
C# Question

Stopping count up timer when restarting game

I've made made the "Roll-a-ball" game from the Unity tutorials on Youtube. However, I've tried program a count up timer, which works fine. The problem is when the game restarts, the timer doesn't, it just continues even though I've set it manually to 00:00.

void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
countText.text = "Count: " + count.ToString ();
winText.text = "";
restartText.text = "";
}

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);

if (Input.GetKeyDown(KeyCode.Space))
{
Vector3 jump = new Vector3(0.0f, 250.0f, 0.0f);

rb.AddForce(jump);
}

if (restart)
{
if (Input.GetKeyUp(KeyCode.R))
{
Application.LoadLevel(Application.loadedLevel);
timerText.text = minutes.ToString("00") + ":" + seconds.ToString("00"); //HERE
}
}
}


void Update()
{
if (count < 14)
{
minutes = (int)(Time.time / 60f);
seconds = (int)(Time.time % 60f);
timerText.text = minutes.ToString("00") + ":" + seconds.ToString("00");
}
}
void OnTriggerEnter(Collider other){
if (other.gameObject.CompareTag ("Pickup")) {
other.gameObject.SetActive (false);
count = count + 1;
countText.text = "Count: " + count.ToString ();
if (count >= 14){
winText.text = "You win!";
restartText.text = "Press 'R' for Restart";
restart = true;

}
}

}

Answer

You are only updating the textbox that displays the output, Time.time will still be the time from when the game first launched.

What you should do is when you create your PlayerController get the current value of Time.time then use that value and subtract it when you are trying to figure out how long the game has been running.

Because you are re-loading the level it should re-initialize the script so you don't need to set the value of the text box when the level restarts.

private float _startTime;

void Start()
{
    rb = GetComponent<Rigidbody>();
    count = 0;
    countText.text = "Count: " + count.ToString ();
    winText.text = "";
    restartText.text = "";   
    _startTime = Time.time; //Save the time the game has been running.
}

void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    rb.AddForce(movement * speed);

    if (Input.GetKeyDown(KeyCode.Space))
    {
        Vector3 jump = new Vector3(0.0f, 250.0f, 0.0f);

        rb.AddForce(jump);
    }

    if (restart)
    {
        if (Input.GetKeyUp(KeyCode.R))
        {
            Application.LoadLevel(Application.loadedLevel);
        }
    }
}


void Update()
{
    if (count < 14)
    {
        minutes = (int)((Time.time - _startTime) / 60f); //Subtract the time from the total time.
        seconds = (int)((Time.time - _startTime) % 60f);
        timerText.text = minutes.ToString("00") + ":" + seconds.ToString("00");
    }
}