So far, you have created two buildings: one that fires projectiles when enemies are within its Area of Engagement and another that continuously generates Gold. In this challenge, you should define a new structure of your own choosing. Below are a few ideas to choose from.
Currently, the cost of each building is set to 50 directly in the code. To give your player more interesting choices, you can make each building have a different cost. To do this, you will create a ScriptableObject.
A ScriptableObject
can be used to define and edit data in the Unity Inspector that does not exist on a Game Object.
Scripts
folder right clickBuildingData
scriptBuildingData
ScriptableObject
menuName
is set to Scriptable Objects/BuildingData
Assets
called Data
Data
folderCreateAssetMenu
attribute in your BuildingData
script.BuildingData
object to TurretData
BuildingData
scriptstring Name
propertyint Cost
propertySimilar to MonoBehaviours you can modify serialized properties in Unity's Inspector.
TurretData
in your Project windowCost
in the InspectorName
in the InspectorGameObject BuildingPrefab
property to BuildingData
When you've finished, your TurretData
should look similar to the image below:
One of the benefits of a ScriptableObject
is the ability to create multiple instances each with their own values.
Data
folder:When you're done, your Data
folder and objects should look similar to the video below:
Your TurretSpawner
has outgrown its original functionality. Its initial purposed was meant to spawn turrets. However, it is time to replace it with a BuildingSpawner
that can be used to place anything defined as a BuildingData
.
TurretSpawner
scriptBuildingSpawner
BuildingSpanwer
BuildingSpawner
to use BuildingDataBuildingSpawner
MonoBehaviourTurretPrefab
propertyBuildingData Selected
Instatiate
call to use the Selected.BuildingPrefab
propertyTurretPrefab
to use Selected.BuildingPrefab
When you have finished, your game should look and act similar to the video below:
Selected.Cost
BuildingSpawner.SpawnTurret
: Controller.Gold
is reduced by Selected.Cost
when the building is placed.Currently, BuildingSpawner
always displays 50 Gold - Place Turret
even when placing non-turret buildings. Additionally, you can build structures that cost too much so long as you have at least 50 gold.
Selected
Not Enough Gold
if the player does not have enough gold to build the selected buildingCanSpawn
to use Selected.Cost
When you have finished, your game should look and act similar to the video below:
Create 2 more buildings of your choice.
In the next lesson, you will add a Tower to your game that takes damage when enemies reach it and, if destroyed, shows a game over screen to the player.