In this section, you will implement an Enemy Spawner that repeatedly spawns new Enemies for your player to destroy!
Before implementing the Enemy Spawner script, create a Prefab that can be placed on your map where Enemies will spawn. Then, place one in your world at the start of your path.
When you're finished, it will look similar to the image below. Note: You can hide your Waypoints from being viewed in the Scene View by clicking the Eye Icon in the Hierarchy next to the parent container. This can be helpful when placing your Enemy Spawner.
EnemySpawner
MonoBehaviour Script with the following properties
Waypoint StartTarget
- This will be the first Target Waypoint for the spawned EnemyEnemyMovement Enemy
- This will be the Enemy that will be spawnedfloat Delay
- This will be the number of seconds between spawnsTo keep your code organized, add a Spawn()
method that creates an Enemy and sets the Enemy's first Target waypoint. To create a Game Object during game play, you can call Object.Instantiate
. This will add the specified Game Object to the Scene.
Spawn()
method
Object.Instantiate
specifying the Enemy
StartingWaypoint
You likely noticed that the Target
property of your EnemyMovement
cannot be set because it is private
. Before your program will compile, you need to modify the EnemyMovement
script to allow the Target
property to be set.
EnemyMovement.Target
property to have a public
setterNote: It is considered a good practice to use private
access modifiers whenever possible. This reduces the potential search space for bugs related to that property being modified. When you change it to public
the property can be accessed anywhere in your code which mean you can introduce bugs related to the property anywhere in your code.
With the EnemyMovement.Target
updated, your EnemySpawner
should now compile
For now, test your Spawn
method by calling it once in Start()
Spawn()
from the EnemySpawner.Start
methodBefore you can test your Spawn()
method, you need give your Prefab the Enemy Spawner component and specify the initial values.
You might be tempted to set the Enemy
property using the Enemy object in your Scene. However, if this object is destroyed, it will break your Spawner. Instead, you can use a Prefab from your Project which won't be destroyed in the Scene.
With your Enemy Spawner wired up, enter Play Mode to verify that a new Enemy is spawned. If all went well, you should see a new Enemy(Clone) in your Hierarchy as well as an Enemy moving between your Waypoints.
You can use Unity's MonoBehaviour.InvokeRepeating
method to repeatedly call a method by its name.
EnemySpawner.Start()
call InvokeRepeating
on the Spawn
method
nameof(Spawn)
rather than a hard coded string.Enter Play Mode to test your Enemy Spawner. You may find it helpful to set the Delay
to a smaller number for testing. Note: Because you use Delay
in your Start
method, you cannot change the Delay
of the Enemy Spawner while in Play Mode.
If all went well, your game should look similar to the video below:
In this challenge, you will update your Enemy Spawner to only spawn a specified number of Enemies. After the specified number of Enemies have spawned, you will use MonoBehaviour.CancelInvoke()
to stop the EnemySpawner
.
SpawnsRemaining
to your EnemySpawner
Spawn()
method, decrement SpawnsRemaining
SpawnsRemaining
is 0
, call CancelInvoke()
When you have successfully implemented this challenge, your game should look similar to the video below.
If the EnemySpawner
becomes disabled, InvokeRepeating
will continue to execute. This is typically not the desired behaviour. It is considered a best practice to call CancelInvoke()
in your OnDisable()
method to prevent strange side effects that might occur when using InvokeRepeating
.
OnDisable()
to your EnemySpawner
CancelInvoke()
in OnDisable()
Verify that your enemies stop spawning if you disable the EnemySpawner
:
You may have noticed that when you re-enable the Enemy Spawner, the remaining Enemies do not continue spawning. This is because Start()
is only called once per Game Object the first time it becomes active in the Scene. If you would like to run code every time a Game Object becomes active, you an use OnEnable()
.
EnemySpawner
to have an OnEnable
methodOnEnable
to call InvokeRepeating
instead of the Start()
methodWhen implemented correctly, your game should act similar to the video below:
Now that you are able to spawn enemies into you game. It is time to design a turret that rotates to follow nearby enemies: 10 - Rotating Turret