Timers, floats, how do I set the Timer to become the time of the previous round?

Hey!

I’m trying to do something that seems pretty simple in my head, but once I try and do it, I’m Not 100% sure how to. Been trying for while now but hope someone can help!

Overview:

I have 2 Buttons:
Button A = Stops the round and Time
Button B = Starts the next round with the NEW Time

HELP with this:
The 1st game round is 10 seconds (normalTime) and once I click “Button A” my time pauses(works).
Then I want to click “Button B”, it will start the next round, HOWEVER it will be the time that I needed in the last round.

Meaning:

  • Round 1: 10 Seconds standard (I finished it in 9 seconds, meaning round 2 is 9 seconds)
  • Round 2: 9 seconds due to previous round ( finished it in 3 seconds though, round 3 is 3 seconds
  • Round 3: 3 seconds etc.

I am not sure how to set variables to one another, when I try normalTime = elapsedTime; after the button was clicked it just sets them both to 0. So, how would I do this best?

Thank you!!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class MyTimer : MonoBehaviour
{
    // currently on public to see information
    public float normalTime = 10.0f;    // My Timer
    public float startTime = 10.0f;        // Variable to have the round length
    public float elapsedTime;

    // Shows the Timer
    public Text text;

    // Shows that Timer is running, if False then normalTime is stopped.
    public bool timerOn = true;

    // Button was clicked
    public bool roundEnd = false;


    void Start ()
    {
        roundEnd = false;
    }

    void Update ()
    {
        if (timerOn)
        {
            normalTime -= Time.deltaTime;
        }

//        text.text = "Time Left: " + Mathf.Round (timeLeft);      // Round Number to full Numbers
        text.text = "Time Left: " + Mathf.Abs (normalTime);      // Shows Exact Time (with multiple decimals)

        if (normalTime < 0)
        {
            SceneManager.LoadScene ("GameOver");
        }
    }


    public void ButtonWasClicked ()
    {
    //    startTime -= normalTime = elapsedTime;
        //normalTime = elapsedTime;
        roundEnd = true;
        print (" ==== BUTTON PRESSED ==== " + "Time Left: " + normalTime);
        timerOn = false;
   
    }

    public void NextRound ()
    {
        print ("ACTION");
    }

I didn’t test this - but here you go.

using System;
using UnityEngine;
using UnityEngine.UI;

public class MyTimer : MonoBehaviour {
    [SerializeField]
    private Text text;

    private bool timerOn = false;
   
    private float previousRoundTime = 10f;
    private float currentRoundTime = 10f;

    // Use this for initialization
    void Start () {
        StartNextRound();
    }
   
    // Update is called once per frame
    void Update () {
        if (!timerOn) {
            return;
        }
        currentRoundTime -= Time.deltaTime;
        text.text = "Time Left: " + Math.Round(currentRoundTime, 2);
        if (currentRoundTime <= 0) {
            // Ran out of time.
            text.text = "You ran out of time.";
            currentRoundTime = 0;
            timerOn = false;
        }
    }

    public void EndRound() {
        timerOn = false;
        previousRoundTime = previousRoundTime - currentRoundTime;
    }
    public void StartNextRound() {
        timerOn = true;
        currentRoundTime = previousRoundTime;
    }
}
1 Like

Hey,
Just had the time to test it and it works perfectly!
I can finally continue my Project :slight_smile:

Thanks a lot Simple-Blue,
Appreciate it loads!