Currently, in your game, the player starts with a limited amount of gold that they can spend to build Turrets. Unfortunately, the player has no way to generate additional resources. Next, you will create a new type of building that the player can place that will generate Gold for them over time.
When you are finished, you should have a GoldGenerator Prefab that you can place on any tile in your game scene:
GoldGenerator
float Delay
property which represents how often it will generate goldint Gold
property which represents how much gold is generatedGoldGenertor
component to your GoldGenerator prefabDelay
to 2 and Gold
to 1When you've finished your prefab should look similar to the image below:
To be able to update the player's gold, the GoldGenerator
requires a reference to the PlayerController
. You will not be able to set this in the Prefab because the PlayerController does not exist here. One way you can acquire a reference to it without the need to set it in the Inspector is to assume that the PlayerController will be a parent of the GoldGenerator. Then, use GetComponentInParent
to set the reference.
PlayerController Controller
property to your GoldGenerator
Awake()
methodAwake()
use GetComponentInParent<PlayerController>()
to assign Controller
GoldGenerator
to your game scene as a child of the PlayerController
PlayerController
property is set when the game startsInvokeRepeating
and CancelInvoke
to spawn enemies into your gameGenerateGold()
method to your GoldGenerator
MonoBehaviourInvokeRepeating
to cause GenerateGold()
to execute every Delay
secondsCancelInvoke
when the GoldGenerator is disabledWhen you're finished, your game should look similar to the video below:
The TurretSpawner you created previously does almost everything you need to be able to spawn GoldGenerators into your game.
If you investigate the error, you will find that the newly spawned GoldGenerator
does not have a PlayerController
. This is because the object is being instantiated without a parent.
The Instatiate
method has several overloaded versions that allow you to specify additional information about how to create the specified GameObject. One version allows you to specify the parent object: Instantiate(GameObject, Transform)
TurretSpawner.SpawnTurret
to set the parent of the object of the turret to be the PlayerController's transform when it is spawned. (Controller.transform
)Currently, you only have a single Build button that enables the TurretSpawner
game which knows references your GoldGenerator prefab. Next, you will update the Build button to set the prefab.
TurretSpawner.TurretPrefab
field to have a public setterTurretSpanwer.TurretPrefab
propertyCurrently, your player is only capable of building Turrets when they click the Build button. Can you add another button that allows them to create generators?
When you have finished, your game should look and act similar to the video below:
Although it is now possible to spawn different types of buildings/turrets, you are not able to easily change the cost (the gold cost of 50 is hard coded). Additionally, the user hints could be more descriptive based on the type of building you want to place. In the next lesson, you will create a ScriptableObject building type that will allow you to quickly and easily create new building types and easily add them to your player's UI.