Keep the Bridge from Restarting
To keep the timer from constantly restarting, you’ll need to add another variable that will control whether or not startTimer()
is allowed to be called again. This variable will use a new type of value called a boolean. Booleans are written the same way as other variables, but instead of using numbers or strings, they can only be set to true or false.
In this situation, the script will use a boolean to check if the timer is currently running before starting it.
- At the top of your script under your variables, create a variable named
timerActive
and set it tofalse
since nobody has pressed the button yet.
- To makes sure
startTimer()
only starts whentimerActive
is false, add a second condition to the if statement inbuttonPressed()
.
- To keep
startTimer()
from restarting before the timer runs out, set the booleantimerActive
totrue
.
- Playtest the game and make sure the timer doesn’t restart anymore. While the timer doesn’t restart, it also can’t be used more than once. To make the button reusable, you’ll reset the boolean in the next section.
Troubleshooting Tips
Issue: Timer changes numbers without counting down:
- Check that you’ve added a second condition in
buttonPressed
's if statement. - Make sure at the beginning of
startTimer()
that you settimerActive
totrue
.
Reset the Timer
To finish the script, you’ll change the boolean and bridge properties back to what they were at the start of the game so the bridge can be used more than once.
- In the
startTimer()
function, after the for loop, set the bridge back to its original properties by changing the bridge’s transparency to0.8
and CanCollide tofalse
.
- Right now the display text stays at 0. To make it blank again, change the
timerText
to an empty string.
- Since you’re now done with the timer, set the
timerActive
boolean tofalse
. This will also allow the timer to restart next time the button is touched.
- Playtest and check to make sure the bridge can be used multiple times.