Search Unity

[Solved] Question - Countdown Time

Discussion in 'Scripting' started by kholyphoenix1, Nov 19, 2015.

  1. kholyphoenix1

    kholyphoenix1

    Joined:
    Apr 17, 2015
    Posts:
    57
    Hello,

    I have a "script" to display "score" and "time".
    I do not know programmed to display the time being reduced.

    Could someone help me?
    Thanks a lot!

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class ShowSystem : MonoBehaviour {
    7.  
    8. // Variables
    9.  
    10.   // RigidBody
    11.   private Rigidbody rb;
    12.   public float speed;
    13.  
    14.   // Check Scene
    15.   public Text TextSceneName;
    16.   public string textSceneName;
    17.  
    18.   // Score
    19.   public Text TextScore;
    20.   private int counterScore;
    21.  
    22.   // Timer
    23.   public Text TextTimer;
    24.   public float counterTimer;
    25.  
    26.  
    27. // Play
    28.  
    29.   void Start ()
    30.   {
    31.  
    32.   // Check Scene
    33.   textSceneName = Application.loadedLevelName;
    34.  
    35.   if (textSceneName == "Maze.001")
    36.   {
    37.  
    38.   //collectionStart = 0;
    39.   //collectionEnd = 12;
    40.   SetTextSceneName ();
    41.   counterTimer = 300;
    42.  
    43.   }
    44.  
    45.      // RigidBody
    46.   rb = GetComponent<Rigidbody>();
    47.  
    48.   // Score
    49.   counterScore = 0;
    50.   SetTextScore ();
    51.  
    52.   // Timer
    53.   counterTimer = 0;
    54.   SetTextTimer ();
    55.  
    56.   }
    57.  
    58. // Update
    59.  
    60.   void Update () {
    61.  
    62.   // Collection
    63.   // ...
    64.  
    65.   // Timer
    66.   counterTimer -= Time.deltaTime;
    67.  
    68.   if (counterTimer <= 0)
    69.   {
    70.  
    71.       counterTimer = 0;
    72.  
    73.   } else {
    74.  
    75.   SetTextTimer ();
    76.  
    77.   }
    78.  
    79.   }
    80.  
    81. // RigidBody
    82.  
    83.   void FixedUpdate ()
    84.   {
    85.  
    86.   float moveHorizontal = Input.GetAxis ("Horizontal");
    87.   float moveVertical = Input.GetAxis ("Vertical");
    88.   Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    89.   rb.AddForce (movement * speed);
    90.  
    91.   }
    92.  
    93. // Collection
    94.  
    95.   void OnTriggerEnter(Collider other)
    96.   {
    97.  
    98.   if (other.gameObject.CompareTag ("Yellow Star"))
    99.   {
    100.  
    101.   other.gameObject.SetActive (false);
    102.   counterScore = counterScore + 5;
    103.   SetTextScore ();
    104.  
    105.   }
    106.  
    107.   if (other.gameObject.CompareTag ("Red Gem"))
    108.   {
    109.  
    110.   other.gameObject.SetActive (false);
    111.   counterScore = counterScore + 50;
    112.   SetTextScore ();
    113.  
    114.   }
    115.  
    116.   if (other.gameObject.CompareTag ("Gold Key"))
    117.   {
    118.  
    119.   other.gameObject.SetActive (false);
    120.   counterScore = counterScore + 500;
    121.   SetTextScore ();
    122.  
    123.   }
    124.  
    125.   }
    126.  
    127. // Show Text Cavans
    128.  
    129.   // Score
    130.   void SetTextScore ()
    131.   {
    132.  
    133.   // Show Text Score
    134.   TextScore.text = "Score: " + counterScore.ToString ();
    135.  
    136.   }
    137.  
    138.   // Timer
    139.   void SetTextTimer ()
    140.   {
    141.  
    142.   TextTimer.text = "Timer: " + counterTimer.ToString ();
    143.  
    144.   }
    145.  
    146.   // Scene
    147.   void SetTextSceneName ()
    148.   {
    149.  
    150.   //TextSceneName.text = "Level: " + textSceneName.toString ();
    151.  
    152.   }
    153.  
    154. }
    155.  
    Solved...
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ShowSystem : MonoBehaviour {
    6.  
    7. // Variables
    8.  
    9.     // RigidBody
    10.     private Rigidbody rb;
    11.     public float speed;
    12.  
    13.     // Check Scene
    14.     public Text TextSceneName;
    15.     public string textSceneName;
    16.  
    17.     // Score
    18.     public Text TextScore;
    19.     private int counterScore;
    20.  
    21.     // Timer
    22.     public Text TextTimer;
    23.     public float counterTimer;
    24.  
    25.  
    26. // Play
    27.  
    28.     void Start ()
    29.     {
    30.  
    31.         // RigidBody
    32.         rb = GetComponent<Rigidbody>();
    33.  
    34.         // Check Scene      
    35.         textSceneName = Application.loadedLevelName;
    36.  
    37.         if (textSceneName == "Maze.001")
    38.         {
    39.  
    40.         // Collection
    41.             //collectionStart = 0;
    42.             //collectionEnd = 12;
    43.            
    44.         // Sceme
    45.             SetTextSceneName ();
    46.             counterTimer = 300f;
    47.  
    48.         // Score
    49.             counterScore = 0;
    50.             SetTextScore ();
    51.  
    52.         }
    53.  
    54.     }
    55.  
    56. // Update
    57.  
    58.     void Update () {
    59.  
    60.         // Collection
    61.         // ...
    62.  
    63.         // Timer
    64.         counterTimer -= Time.deltaTime;
    65.  
    66.         if (counterTimer <= 0)
    67.         {
    68.    
    69.            counterTimer = 0;
    70.    
    71.         } else {
    72.  
    73.             SetTextTimer ();
    74.  
    75.         }
    76.  
    77.     }
    78.  
    79. // RigidBody
    80.  
    81.     void FixedUpdate ()
    82.     {
    83.  
    84.         float moveHorizontal = Input.GetAxis ("Horizontal");
    85.         float moveVertical = Input.GetAxis ("Vertical");
    86.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    87.         rb.AddForce (movement * speed);
    88.  
    89.     }
    90.  
    91. // Collection
    92.  
    93.     void OnTriggerEnter(Collider other)
    94.     {
    95.  
    96.         if (other.gameObject.CompareTag ("Yellow Star"))
    97.         {
    98.  
    99.             other.gameObject.SetActive (false);
    100.             counterScore = counterScore + 5;
    101.             SetTextScore ();
    102.  
    103.         }
    104.  
    105.         if (other.gameObject.CompareTag ("Red Gem"))
    106.         {
    107.  
    108.             other.gameObject.SetActive (false);
    109.             counterScore = counterScore + 50;
    110.             SetTextScore ();
    111.  
    112.         }
    113.  
    114.         if (other.gameObject.CompareTag ("Gold Key"))
    115.         {
    116.  
    117.             other.gameObject.SetActive (false);
    118.             counterScore = counterScore + 500;
    119.             SetTextScore ();
    120.  
    121.         }
    122.  
    123.     }
    124.  
    125. // Show Text Cavans
    126.  
    127.     // Score
    128.     void SetTextScore ()
    129.     {
    130.  
    131.         // Show Text Score
    132.         TextScore.text = "Score: " + counterScore.ToString ();
    133.  
    134.     }
    135.  
    136.     // Timer
    137.     void SetTextTimer ()
    138.     {
    139.  
    140.         TextTimer.text = "Timer: " + counterTimer.ToString ();
    141.  
    142.     }
    143.  
    144.     // Scene
    145.     void SetTextSceneName ()
    146.     {
    147.  
    148.         //TextSceneName.text = "Level: " + textSceneName.toString ();
    149.  
    150.     }
    151.  
    152. }
     
    Last edited: Nov 19, 2015