Search Unity

How to set and count Time and save record

Discussion in 'iOS and tvOS' started by webphone, Jul 1, 2010.

  1. webphone

    webphone

    Joined:
    Mar 13, 2010
    Posts:
    315
    I would like to set a timer when game level starts, when the player wins the game level, the timer stops.

    The time period will be recorded and display everytime when game finishes.

    So, the lowest time to complete a game level is the best result. Just like Pool Ninja.

    How can I code it?
     
  2. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    http://unity3d.com/support/documentation/ScriptReference/Time.html

    Create a float variable that is going to keep track on time.

    When the level/game starts, you set the variable to 0. In every Update your then just add the deltaTime.

    Code (csharp):
    1.  
    2. var levelTimer : float;
    3. var updateTimer : boolean;
    4.  
    5. function Start()
    6. {
    7.    levelTimer = 0.0f;
    8. }
    9.  
    10. function Update()
    11. {
    12.    if (updateTimer)
    13.       levelTimer += Time.deltaTime;
    14. }
    15.  
    16. function LevelEnded()
    17. {
    18.    updateTimer = false;
    19. }
    20.  
    Something along those lines.
     
  3. webphone

    webphone

    Joined:
    Mar 13, 2010
    Posts:
    315
    may I ask?

    1) The Time.deltaTime output 6 decimal places, how can I round it up to 2 decimal places?

    2) How to store the levelTimer value in a local game?
     
  4. SJAM

    SJAM

    Joined:
    Jan 11, 2009
    Posts:
    729
    If you want in second try this :

    Code (csharp):
    1. var timerInSecond = 0;
    2. private var levelTimer = 0.0;
    3. private var updateTimer = false;
    4.  
    5. function Start()
    6. {
    7.    updateTimer = true;
    8.    levelTimer = 0.0;
    9. }
    10.  
    11. function Update()
    12. {
    13.    if (updateTimer)  
    14.       levelTimer += Time.deltaTime*1;
    15.  
    16.  
    17.  /// float to int
    18.  timerInSecond = Mathf.Round (levelTimer);
    19. }
    20.  
    21. function LevelEnded()
    22. {
    23.    updateTimer = false;
    24.  
    25.    ///Save Time
    26.    PlayerPrefs.SetInt("Time In Second", timerInSecond );
    27. }
    28.  
     
  5. webphone

    webphone

    Joined:
    Mar 13, 2010
    Posts:
    315
  6. SJAM

    SJAM

    Joined:
    Jan 11, 2009
    Posts:
    729
    Ok, I found an other way

    Code (csharp):
    1. var timerTex = ""; //Text for GUI
    2. private var levelTimer = 0.0;
    3. private var updateTimer = false;
    4.  
    5. function Start()
    6. {
    7.    updateTimer = true;
    8.    levelTimer = 0.0;
    9. }
    10.  
    11. function Update()
    12. {
    13.    if (updateTimer)
    14.       levelTimer += Time.deltaTime*1;
    15.       timerTex = levelTimer.ToString("f2"); //Time to texture with 2 decimal
    16. }
    17.  
    18. function LevelEnded()
    19. {
    20.    updateTimer = false;
    21.    PlayerPrefs.SetFloat("Time In Second", levelTimer ); //Keep Complete Time for compare and save
    22. }
    Enjoy !!
     
  7. unity_BmFFV4qGhePyjA

    unity_BmFFV4qGhePyjA

    Joined:
    Sep 23, 2017
    Posts:
    5
    "var may only appear within a local variable declaration " how do i fix this?



    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.UI;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour {
    7.  
    8.     public float speed;
    9.     public Text countText;
    10.     public Text winText;
    11.  
    12.     private Rigidbody rb;
    13.     private int count;
    14.     private var levelTimer = 0.0;
    15.     private var updateTimer = false;
    16.  
    17.     void Start ()
    18.     {
    19.         rb = GetComponent<Rigidbody> ();
    20.         count = 0;
    21.         SetCountText ();
    22.         winText.text = "";
    23.         var timerTex = "";
    24.         updateTimer = true;
    25.         levelTimer = 0.0;
    26.    
    27.     }
    28.  
    29.     void FixedUpdate ()
    30.     {
    31.         float moveHorizontal = Input.GetAxis ("Horizontal");
    32.         float moveVertical = Input.GetAxis ("Vertical");
    33.  
    34.         Vector3 movement = new Vector3 (moveHorizontal * speed, 0.0f, moveVertical * speed);
    35.  
    36.         rb.AddForce (movement);
    37.  
    38.         if (updateTimer)
    39.                 levelTimer += Time.deltaTime*1;
    40.             timerTex = levelTimer.ToString("f2");
    41.     }
    42.  
    43.     void OnTriggerEnter(Collider other)
    44.     {
    45.         if (other.gameObject.CompareTag ("Pick Up"))
    46.         {
    47.             other.gameObject.SetActive (false);
    48.             count = count + 25;
    49.             SetCountText ();
    50.         }
    51.     }
    52.  
    53.     void SetCountText ()
    54.     {
    55.         countText.text = "Count: " + count.ToString ();
    56.         if (count >= 325) {
    57.             winText.text = "You Win";
    58.  
    59.             updateTimer = false;
    60.             PlayerPrefs.SetFloat("Time In Second", levelTimer );
    61.    
    62.         }
    63.     }
    64. }
    65.  
     
  8. JoeSchroom

    JoeSchroom

    Joined:
    Mar 17, 2018
    Posts:
    2
    try setting your variables to public instead of private.