Instantiating and Destroying in Unity

Bob Hilbig
4 min readJul 7, 2021

In the process of making a 2d Space Shooter we must give ourselves something to shoot. We can’t start off with 1000 enemies flying at the player, and we can’t start off with 1000 bullets to fire at the enemies. Where do we put these objects so that we can call upon them whenever we need them? Let’s create a base from which to start, called a prefab.

Basic Blaster Bullet. Take two for upset stomachs.

A prefab acts as a template from which all others are made. In this case, we plan on shooting a lot of enemies so let’s create a system to shoot two at a time.

First, consider how are going to let the game know to fire a weapon. For that we use the Input Manager and find Fire1. We can use the default or create our own axis to call the firing script.

Press the big red button. Do it!

From this we see that we can press the left ctrl button or mouse 0 (the left mouse button). We also can use a controller input just as easy.

Ok so we detect our fire button, and fire our blaster. But what is that second part of the if statement. _canFire is a float variable that is used to compare time in between shots fired. We used GetButton so that we can hold down the button to fire if we wanted to.

So every time we fire a weapon we have to wait a set amount of time before we fire a new weapon. In the future we can subtract some small amount of time as a powerup to make the weapons fire faster. Eventually we will let the weapon type decide the fire rate.

I said we were going fire two at a time. We will tell Unity to create them using the Instantiate function. First, what are the things we are instantiating.

Lets put our Basic Blaster Prefab into the _basicBlasterPrefab using the SerializeField keyword and the Inspector.

Use this power only for good.

Now we Instantiate the prefab. Give it a position, which is straightforward. Finally give it a rotation. Quaternions are pretty complicated, but the identity is essentially setting the rotation variables to 0 so you are left with a object that is facing the same way as the base prefab.

The prefab has a script that has its own update function for movement as well as variables for damage. We can use that damage to start destroying enemies. This part is pretty straightforward.

He’s coming straight for us!

First be are going to use area triggers to decide when a blaster interacts with an enemy. In the Blaster Prefab lets check on the Collider settings Is Trigger. Then lets do the same for the Enemy Prefab. We can now code with that. First, the Blaster interaction.

What keeps hitting me?

Let’s detect what is hitting us. If its the weapon, get the weapon damage from that weapon and apply it to ourselves. Then destroy that blaster bullet, it has done its job. If we lose all our hit points then we destroy ourselves.

Ramming speed!

If the enemy manages to hit the player, then the player should take damage. We have a variable for that so lets do some damage. Then destroy the enemy. The null reference check is important to prevent crashes. The player has its own update call that if its hit points are reduced to zero then it also gets destroyed. This could cause null refs which are bad, and should be avoided or handled.

Triumph! We destroyed the enemy!

Instantiation and Destruction. Both need to be done in order and with planning.

--

--