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
timerActiveand set it to
falsesince nobody has pressed the button yet.
- To makes sure
startTimer()only starts when
timerActiveis false, add a second condition to the if statement in
- To keep
startTimer()from restarting before the timer runs out, set the boolean
- 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.
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 set
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 to
0.8and CanCollide to
- Right now the display text stays at 0. To make it blank again, change the
timerTextto an empty string.
- Since you’re now done with the timer, set the
false. 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.
Finished Project Sample
Download the finished project here.