Search Unity

WaitForSeconds(1) appears to be counting 2 secs every 1 sec?

Discussion in 'Scripting' started by FirstAndTen, Aug 2, 2019.

  1. FirstAndTen

    FirstAndTen

    Joined:
    Oct 16, 2017
    Posts:
    16
    Hi - I have some code in my game that updates a countdown timer on the screen. This code has been in for a long time, over a year and was working until today. I updated to 2019.2 yesterday but not sure if that's related. The countdown timer appeared to all of a sudden be counting down 2 seconds every 1 second. 40...38...36...34...32...etc when before it was 40..39..38..37..etc as I would expect. It doesn't update every 2 secs, it updates every 1 second but with 2 sec jumps.

    I put Debug.Log in the calls where it formats the seconds and updates the UI. I have found that it appears to now count 2 seconds every 1 sec with the timestamps on the Debug.Log calls to prove it. I've spent 2 hours trying to figure this out. Hoping someone has some insight as to why it would start doing this now?

    Capture1.PNG

    Here's a screenshots of the code:

    Capture2.PNG
    Capture3.PNG
    Capture4.PNG

    Any help is appreciated. Thanks.
     
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    is CurrentSeconds static? Maybe you have 2 instances in your scene? You can right click the script in your project view and > Find all references in scene
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    in the picture you posted it seems to work fine.. counts one by one.
     
  4. FirstAndTen

    FirstAndTen

    Joined:
    Oct 16, 2017
    Posts:
    16
    If you look at the timestamps every 2 seconds has the same 1 second timestamp.
     
  5. FirstAndTen

    FirstAndTen

    Joined:
    Oct 16, 2017
    Posts:
    16
    Good idea, let me make sure I'm only dealing with one instance.

    Update: I verified there's only one instance. I'm out of ideas.
     
    Last edited: Aug 2, 2019
  6. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    Just to make sure, have you verified at runtime? As in rightclick the script and find references in scene while the game is running and you're experiencing the double countdown.
     
  7. FirstAndTen

    FirstAndTen

    Joined:
    Oct 16, 2017
    Posts:
    16
    Yes sir, there's only one instance when I do what you advised both running the game and when it's not running. I also tried changing it over to a static instance class and it's still behaving the same way. Could this be a bug in 2019.2?
     
  8. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    The only option left I can see is that you are running DecreaseTime twice. Can you post the entire class that contains the DecreaseTime coroutine?
     
  9. FirstAndTen

    FirstAndTen

    Joined:
    Oct 16, 2017
    Posts:
    16
    I created a brand new project and tested a countdown timer and it works fine so it's not a bug in Unity. It's obviously getting called twice but I'm not sure how. Here's the class:

    Code (CSharp):
    1. public class Clock : MonoBehaviour
    2. {
    3.     static public Clock Instance;
    4.  
    5.     public int TotalSeconds;  
    6.     private int CurrentSeconds;
    7.     public ClockType ClockType;
    8.  
    9.     void Awake()
    10.     {
    11.         Instance = this;
    12.     }
    13.  
    14.     void Start()
    15.     {
    16.         this.ClockType = ClockType.Play;      
    17.         CurrentSeconds = 40;
    18.         FormatUIClockDisplay();
    19.     }
    20.  
    21.     public void StartClock()
    22.     {
    23.         Time.timeScale = 1;
    24.         StartCoroutine("DecreaseTime");
    25.     }
    26.    
    27.     public void StopClock()
    28.     {
    29.         StopCoroutine("DecreaseTime");
    30.     }
    31.  
    32.     public void Update()
    33.     {
    34.        
    35.     }
    36.  
    37.     public void Reset()
    38.     {
    39.         CurrentSeconds = 40;
    40.     }
    41.  
    42.     public void FormatUIClockDisplay()
    43.     {
    44.         Debug.Log("Calling Scoreboard to update to: " + CurrentSeconds);
    45.         UIManager.Instance.Scoreboard.SetPlayClock(string.Format("{0}", CurrentSeconds));
    46.                
    47.     }  
    48.  
    49.     IEnumerator DecreaseTime()
    50.     {
    51.         while(true)
    52.         {
    53.             yield return new WaitForSeconds(1);
    54.             CurrentSeconds--;
    55.  
    56.             FormatUIClockDisplay();
    57.             if (CurrentSeconds == 0)
    58.             {
    59.                 this.StopClock();
    60.                 GameFlowManager.Instance.ClockHasExpired(this.ClockType);
    61.             }
    62.         }
    63.     }
    64. }
     
  10. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    I'm guessing StartClock gets called twice. Try to put StopAllCoroutines(); right before StartCoroutine(DecreaseTime());

    At least you'll know if that's the culprit. This will prevent the routine from running twice at the same time, at least in a single instance of the class.
     
  11. FirstAndTen

    FirstAndTen

    Joined:
    Oct 16, 2017
    Posts:
    16
    Also I did a search through the entire project for references to DecreaseTime and found 3, all in this class:

    Capture5.PNG

    And here's the view when the game is running and I do a "Find References in Scene":

    Capture6.PNG
     
  12. FirstAndTen

    FirstAndTen

    Joined:
    Oct 16, 2017
    Posts:
    16
    StopAllCoroutines() made it stop skipping. I did a project search for StartClock() and it only found 1 place it's called but that's in an FSM so I need to dig deeper and see if that FSM is somehow getting instantiated twice. At least I'm on the right path. I really appreciate your help! Thank you!
     
    ADNCG likes this.