Search Unity

Timer

Discussion in 'Editor & General Support' started by DiegoCuesta, Dec 2, 2021.

  1. DiegoCuesta

    DiegoCuesta

    Joined:
    Sep 28, 2021
    Posts:
    3
    Hello. I'm trying to add a timer to my unity project. The thing is that when i run the game, and the time ends, the game doesn't ends. I've watched lots of tutorials but none seems to work. Now, when i run the game (it should be 90 sec) there's just a big 90 on the screen, time doesn't pass. I dont know what to do. This is the script of the timer. Thanks

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameTimer : MonoBehaviour
    7. {
    8.     public float timeValue = 90;
    9.     public Text timeText;
    10.  
    11.     void Update()
    12.     {
    13.         if (timeValue > 0)
    14.         {
    15.             timeValue -= Time.deltaTime;
    16.         }
    17.          else
    18.         {
    19.             timeValue = 0;
    20.         }
    21.  
    22.         DisplayTime(timeValue);
    23.     }
    24.     void DisplayTime (float timeToDisplay)
    25.     {
    26.         if (timeToDisplay < 0)
    27.         {
    28.             timeToDisplay = 0;
    29.         }
    30.         else if(timeToDisplay > 0)
    31.         {
    32.             timeToDisplay += 1;
    33.         }
    34.  
    35.         float minutes = Mathf.FloorToInt(timeToDisplay / 60);
    36.         float seconds = Mathf.FloorToInt(timeToDisplay % 60);
    37.  
    38.         timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    39.            
    40.     }
    41.  
    42. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    That makes me think perhaps the above code isn't working or connected properly. ARe you seeing any errors at runtime?

    Whatever it is, you must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494