Journal 07 — Create and Destroy in Unity

Chris Nielsen
5 min readApr 25, 2021

--

Two features common in many games is the process of spawning enemies to fight, bullets or lasers that shoot from weapons or spaceships, or even scenery and obstacles that are constantly reproduced in the case of a side-scrolling game like Flappy Bird.

In Unity, we use the command Instantiate:

C# Instantiate Command

In this example, we can spawn an enemy and lasers using the Instantiate command and three parameters:

Object original” — this can be a prefab enemy or laser projectile

Vector3 position” — the location of where to spawn. This could be a random spot in the case of an enemy location, or in the case of bullets or lasers, can be specified at an exact location, like the tip of a gun or ship’s weapon.

Quaternion rotation” — the rotation characteristics of the spawned object. Typically you can set this to Quaternion.identity, which means “no rotation” — the object is perfectly aligned with the world or parent axes.

Let’s create four items: a cube to represent a player, a cube to represent an enemy, a capsule to represent a laser, and an empty GameObject to represent the Spawn_Manager. You can position the laser capsule and resize it, then drag it to the Prefabs folder in your assets, and delete from the Hierarchy (since we can spawn them later). Do the same with the Enemy object. Position it near the top of the screen in front of the Player, and drag to to the Prefabs folder to make it a Prefab, and delete it from the Hierarchy.

Unity Editor and Heirarchy
Unity Hierarchy and Assets with Prefabs created

To spawn the enemies and laser, we we will need C# scripts created for the Enemy, Laser, Player, and SpawnManager. Note the scripts for Enemy and Laser are attached to the Prefabs.

Player movement was already prepared in my Journal 04, so we will add some additional functionality to be able to fire lasers. The basic check in the Update method is when the spacebar is pressed, we want to instantiate a laser prefab in front of the Player. To keep the code clean, we will put the FireLaser code in its own function.

Unity C# variables needed in Player script
Unity C# movement and fire laser called in Update method
Unity C# FireLaser function to instantiate lasers

In the example code, the laser prefab is created or instantiated at the Player position (transform.position). We have added a Vector3 offset variable to allow for adjustment to the laser location so it looks like it is firing from the front of the Player. Some additional variables and code to delay the firing are included in the function (this ensures the Player can’t spam the spacebar and generate too many lasers), but that will be discussed in more detail in a later Journal.

Enemy instantiation is handled by the Spawn_Manager. The Enemy script includes the movement towards the Player.

In the Spawn_Manager, we want to instantiate the enemies in a random spot above the Player. We don’t want to just put the Instantiate code in the Start method because that will only generate one time, and if the Instantiate code is in the Update method, the enemies will continuously spawn with no control.

We want to spawn at preferably a regular timed interval. We can do this with a Coroutine.

Unity C# SpawnManager variables and StartCoroutine
Unity C# IEnumerator to control spawning at 5 second interval

In the example code above, we have included the limits of the game screen in xMin and xMax, and so we are setting a random position to spawn within those limits. We have also specified that the spawned enemies all need to be collected in the Hierarchy under an empty GameObject called _enemyContainer. This will keep the Hierarchy cleaner. At the end of the coroutine is a pause for 5 seconds before the next spawn (yield return new WaitForSeconds(5f);)

Unity Playmode - Enemy Prefabs in Empty_Container
Unity Playmode — Laser Prefabs after pressing spacebar
Unity Playmode - Enemy and Laser Prefabs

We need to use the Destroy command in Unity for a couple game scenarios: The laser prefabs after they have traveled past the game screen should be removed from the game. Similarly, if there is a collision between the enemy and a laser, these two objects should be removed, or the enemy collides with a Player and the Player takes damage, the enemy prefab should also be removed.

In the Laser C# script, which currently handles basic movement of the laser projectile after it is instantiated, we can put the Destroy command to destroy the prefab after it reaches a certain height above the game view.

Unity C# Laser prefab — Destroy

In the Enemy C# script, which handles movement of the enemies after they are instantiated, use of the Destroy command is a little more complicated. We need to check for collisions with the Player and also with a Laser prefab. We will go into more detail on collisions in a later article, but essentially the code will check for an enemy collision with the Player, so that the Player takes some damage and loses a life, and the Enemy prefab is destroyed. Alternatively, if the Enemy collides with a Laser prefab, both the Enemy and Laser are destroyed.

Unity C# Enemy code — movement and collision checks
Unity Playmode — Laser and Enemy Prefab Destroy

I appreciate you stopping by. I’m looking forward to sharing more info.

Thanks!

--

--

Chris Nielsen
Chris Nielsen

Written by Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

No responses yet