Currently, when your player clicks the Build button, it enables the TurretSpawner which, in turn allows them to click on tiles to spawn multiple turrets. Next, you will create a PlayerController
class that will track a Gold
resource for the player that limits their ability to place turrets. When the player builds a turret, it will reduce the player's Gold
.
MonoBehaviour
named PlayerController
int Gold
PropertyPlayerController
component . Gold
property in the Inspector to 200
When you have finished, your scene should look similar to the image below:
To help the player know how much gold they have, add a Text Label to your Canvas
Recall how you anchored your Build button to the bottom left corner of the screen (see: 16 - Placing Turrets > UI Pivots). You can do the same thing with any UI component. Additionally, if you hold Shift and Alt while selecting a pivot, it will move your UI component to that position.
Gold Label
Pos X
and Pos Y
values slightly to keep the text from touching the edge of the screen.Now that you have a Gold Label, you can make it reference your PlayerController
and update the text component to display the current amount of Gold the player has available.
MonoBehaviour
named GoldLabelController
PlayerController Controller
propertyTextMeshProGUI Label
propertyUpdate()
method:Label.text
to display Controller.Gold
GoldLabelController
component to your GoldLabel
PlayerController
and Label
properties in the Inspector
Label
should reference the Gold Label (self)PlayerController
's Gold property to observe the changeTo be able to check if we can spawn a turret, the TurretSpawner must check if the player has enough gold.
PlayerController Controller
property to the TurretSpawner
MonoBehaviourbool CanSpawn(TileController tileController)
method to the TurretSpawner
MonoBehaviour:false
if the tileController
is occupied false
if the Controller
does not have at least 50 goldtrue
SpawnTurret
method to use CanSpawn
to determine if a turret can be spawnedSpawnTurret
method to reduce Controller.Gold
by 50When you have finished, you should see something similar to the video below:
Currently, your player does not receive much information about what the result of clicking on a tile will be. This can be both confusing and frustrating. How were they supposed to know the turret was going to cost 50 gold! Maybe they didn't see that they were out of gold and don't understand why clicking is not placing a turret. To help create a better user experience, you will add in a text label that will display information to the player.
TextMeshProGUI InfoLabel
to your PlayerController
MonoBehaviourStart()
method to PlayerController
InfoLabel.text
to display "Click Build to Place a Turret"By default, Text labels intercept click events of a mouse. This can be useful if you want to detect if a mouse is hovering over a label. However, this can be problematic if your label is blocking a button or your game scene. This can be disabled in the Extra Settings
section of the TextMeshPro - Text(UI)
component.
ShowInfo(TileController)
method to TurretSpawner
:tileController.IsOccupied
display "Cannot build here"Controller.Gold < 50
display "<color=red>Not enough gold"TurretSpanwer
MonoBehaviour in the following waysShowInfo
is called when the cursor enters a tileWhen you have finished, your game should behave similar to the scene below:
Now that your player can use gold to build turrets, it is now time to create a way for the player to gain gold. In the next lesson, you will create a new building that continuously generates gold for the player.