Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by Trickzbunny, Jan 21, 2017.

  1. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    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!!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6. public class MyTimer : MonoBehaviour
    7. {
    8.     // currently on public to see information
    9.     public float normalTime = 10.0f;    // My Timer
    10.     public float startTime = 10.0f;        // Variable to have the round length
    11.     public float elapsedTime;
    12.  
    13.     // Shows the Timer
    14.     public Text text;
    15.  
    16.     // Shows that Timer is running, if False then normalTime is stopped.
    17.     public bool timerOn = true;
    18.  
    19.     // Button was clicked
    20.     public bool roundEnd = false;
    21.  
    22.  
    23.     void Start ()
    24.     {
    25.         roundEnd = false;
    26.     }
    27.  
    28.     void Update ()
    29.     {
    30.         if (timerOn)
    31.         {
    32.             normalTime -= Time.deltaTime;
    33.         }
    34.  
    35. //        text.text = "Time Left: " + Mathf.Round (timeLeft);      // Round Number to full Numbers
    36.         text.text = "Time Left: " + Mathf.Abs (normalTime);      // Shows Exact Time (with multiple decimals)
    37.  
    38.         if (normalTime < 0)
    39.         {
    40.             SceneManager.LoadScene ("GameOver");
    41.         }
    42.     }
    43.  
    44.  
    45.     public void ButtonWasClicked ()
    46.     {
    47.     //    startTime -= normalTime = elapsedTime;
    48.         //normalTime = elapsedTime;
    49.         roundEnd = true;
    50.         print (" ==== BUTTON PRESSED ==== " + "Time Left: " + normalTime);
    51.         timerOn = false;
    52.    
    53.     }
    54.  
    55.     public void NextRound ()
    56.     {
    57.         print ("ACTION");
    58.     }
     
  2. Simple-Blue

    Simple-Blue

    Joined:
    Jan 21, 2017
    Posts:
    5
    I didn't test this - but here you go.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class MyTimer : MonoBehaviour {
    6.     [SerializeField]
    7.     private Text text;
    8.  
    9.     private bool timerOn = false;
    10.    
    11.     private float previousRoundTime = 10f;
    12.     private float currentRoundTime = 10f;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         StartNextRound();
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         if (!timerOn) {
    22.             return;
    23.         }
    24.         currentRoundTime -= Time.deltaTime;
    25.         text.text = "Time Left: " + Math.Round(currentRoundTime, 2);
    26.         if (currentRoundTime <= 0) {
    27.             // Ran out of time.
    28.             text.text = "You ran out of time.";
    29.             currentRoundTime = 0;
    30.             timerOn = false;
    31.         }
    32.     }
    33.  
    34.     public void EndRound() {
    35.         timerOn = false;
    36.         previousRoundTime = previousRoundTime - currentRoundTime;
    37.     }
    38.     public void StartNextRound() {
    39.         timerOn = true;
    40.         currentRoundTime = previousRoundTime;
    41.     }
    42. }
    43.  
     
    Trickzbunny likes this.
  3. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    Hey,
    Just had the time to test it and it works perfectly!
    I can finally continue my Project :)

    Thanks a lot Simple-Blue,
    Appreciate it loads!