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?
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
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.
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?
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:
public class Clock : MonoBehaviour
{
static public Clock Instance;
public int TotalSeconds;
private int CurrentSeconds;
public ClockType ClockType;
void Awake()
{
Instance = this;
}
void Start()
{
this.ClockType = ClockType.Play;
CurrentSeconds = 40;
FormatUIClockDisplay();
}
public void StartClock()
{
Time.timeScale = 1;
StartCoroutine("DecreaseTime");
}
public void StopClock()
{
StopCoroutine("DecreaseTime");
}
public void Update()
{
}
public void Reset()
{
CurrentSeconds = 40;
}
public void FormatUIClockDisplay()
{
Debug.Log("Calling Scoreboard to update to: " + CurrentSeconds);
UIManager.Instance.Scoreboard.SetPlayClock(string.Format("{0}", CurrentSeconds));
}
IEnumerator DecreaseTime()
{
while(true)
{
yield return new WaitForSeconds(1);
CurrentSeconds--;
FormatUIClockDisplay();
if (CurrentSeconds == 0)
{
this.StopClock();
GameFlowManager.Instance.ClockHasExpired(this.ClockType);
}
}
}
}
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.
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!