Tower Defense Tutorial – Brackeys – Ep. 28

March 8, 2019

Tower Defense Tutorial

Episode 28 – Winning Levels

Youtube – How to make a Tower Defense Game (E28 WINNING LEVELS) – Unity Tutorial

By: Brackeys
Ep. 28

This tutorial sets up the system for what happens when the player beats a level.

We began by creating a UI panel for all of the UI that should appear when the player completes a level.

Once we had that setup, we looked into creating some functionality for this screen. This started by creating a RoundsSurvived script to just place on any round counting objects (one in the game over and level won screen) to display the text properly. As an added effect, we wanted to animate the round counter to count up from 0 to give it more of an impact than simply displaying the number.

To get this round counter animation effect, we used an IEnumerator for its ability to WaitForSeconds. We created a while loop that would count from 0 to the number of rounds the player survived one at a time, with a WaitForSeconds of 0.05 each time. This provides the rapid counting effect we’re looking for to spice up the round counter.

The tutorial goes over a bug where enemies could die multiple times because of the fact that Destroy(gameObject) is not a very immediate process within Unity. Since our enemy script just checked if health reached 0 or less to determine if an enemy would call the Die method, which would then destroy it, sometimes this would allow it to call the Die method multiple times if it got shot repeatedly in a small time frame. To solve this, we just added a bool isDead, and have the check to call the Die method see if health is at/below 0 AND !isDead to ensure this is only called once (the Die method sets isDead to true).

PROBLEMS

As indicated in the comments for the tutorial video, there is an issue when selecting the Menu button upon completing a level. The Continue method is where the levelReached Player Pref is updated, so if the player simply selects the Menu button instead, the levelReached is never updated so the player will not be able to select the next level in the level selector.

SOLUTIONS

To keep the levelReached and levelToUnlock variable in the CompleteLevel script (as opposed to the Game Manager) and fix this issue in as clean a way as possible, the comment suggested moving these variable updates to an OnEnable method. This is nicer than just adding it to both methods, and keeps it within the same script which is helpful if I intend on making the GameManager into a prefab in the future.

SUMMARY

  • Rule of Thumb: “Bigger” highlight animations for bigger buttons
  • In Unity, if you want your button to be just text, you can also drag the Text element of it into the Target Graphic of the Button (script) so the text is the specifically clickable area
  • IEnumerators are used when you want a method that can be paused to continue at a later time (can be as small as a frame or two)
  • Destroy(gameObject) in Unity is SLOW. This is always a good piece of code to look around if you are having strange bugs/issues.