Timer not counting down

Hi all,

after creating my previous quite basic text101 ‘game’, I decided to try and expand my code and make things a bit more complicated.
I’m now up to 30 different states before you even complete the puzzle to get out of the first cell and I can’t wait to share this attempt!
The next part of my puzzle requires a 20 second countdown timer.
Here is my code
What should be happening is that the when we get to screen2, bedtime is then set to 20 and the countdown starts.
What actually happens, is that it doesn’t count down, it stays on 20.

If, when I declare the float and set it’s value at that point, then the countdown DOES work.
I tried to move line 21 ( if (bedtime > 0) {bedtimer ();} ) to the line underneath where I give bedtime the value, but this doesn’t work either.
I just can’t get my head around why this doesn’t work. I’m sure it’s something simple but the logic escapes me.
Can anyone explain why this doesn’t work and help me find a solution?
Thanks

Hi Robert,

The reason this is happening is because the Update() method is called every frame.

So, once the state is correct for screen2 you end up in that method which sets the countdown to 20f. Then the next frame, the state is still screen2, so it sets the countdown to 20f… and repeat…

You could consider adding an Initialise() method which you call from Start() and place any custom game config in there, such as setting the countdown. As Start() is only called the once, Initialise() would also only be called the once. This would be a tidier technique than setting the values at the beginning when you declare the variables.

Hope this helps :slight_smile:

Thanks Rob,

Now you’ve pointed out that the countdown is being reset back to 20f, it’s blatantly obvious and I can’t believe I didn’t realise that before.
Thanks

1 Like

No problem, it happens, all part of the learning curve. :slight_smile: