Instantiating & Destroying Gameobjects in Unity

Make’em and Brake’em

Marc Frappier
5 min readApr 7, 2021

Instantiating an object

Instantiation is the spawning of an object, normally a clone of a prefab object, with a trigger. This could be a key press, a mouse button or even a script.

Since we are going to use a prefab object, we need to create the object, and place it into a prefab folder. Lets also resize the object, setting the scale to 0.2 for x, y and z.

Create the GameObject and place in Prefab folder

Now that we have the laser in the prefab folder, we can delete the laser in the Hierarchy.

Deleting GameObject from Heirarchy

Notice we still have the Laser object in the Prefab folder. Now we need for it to appear in our scene with some sort of trigger. We will use a space press to instantiate the laser object.

We will do this in our Player script. In the Update method, we first need to check if the space key has been pressed. If this is true, then we make our laser appear.

First off, we need to create the instance of the GameObject in the Player script by creating a GameObject variable. This will allow us to place the Laser object into the Player scrips in Unity.

Creating GameObject variable for Laser object

The [SerializeField] will allow us to manipulate this variable from the Unity editor.

We need to join the laser object with the Player object. We do this by dragging the laser from the Prefab folder into the Laser Prefab slot in the Player (Script) section of the Player Inspector.

Now we can instantiate our laser object from within our Player script.

Instantiating Laser object

We can now see how when we press the space key on our keyboard, a laser object is instantiated.

That is great, but it would be nice if the object would behave like a laser and move upwards.

To do this, we need to create a script for our laser object and pair it up with our laser.

Creating Laser Script and pairing it with Laser Prefab

Now our Laser Prefab has the Laser script tied to it.

To make our Laser object move upwards, we are going to need to know the speed at which it will be moving. If we don’t declare a speed, the laser will either be moving at 60 mt/s or 1 mt/s if we use Time.deltatime. So, lets create a serialized variable for our laser’s speed.

Now, in the Update method we can make the laser move upwards using transform.Translate.

Here we are telling Unity we want the laser object to move up at a speed of 8 m/s.

Firring the laser

Awesome.

There are a few things we can improve here:

  1. Notice how the laser object is coming from inside our Player object. To remedy this we need to offset the starting position of the object when it is spawned.

To do this we just need to add a new Vector3 to the transform.position in the Instantiate script.

Offsetting the instantiation of the laser object from the Player object

We are offsetting the object by 0.8f on the y axis. Now when we press the space key, the laser object is instantiated 0.8f above our player object.

2. Another problem is that each time we fire a laser, a new object is spawned and it is filling up our Hierarchy with laser objects. This can fill up our memory quite fast. To remedy this, we need to remove or destroy these objects once they have moved off scene.

Destroying an object

To destroy the object, we need to figure out at what point we want the object destroyed. In our example, the laser moves off scene at a Y coordinate of 8f.

So, in our Laser script, after the moving-up code, we can add an if statement that will check the position of the laser object. When the condition is true, the object is destroyed.

Destroying a gameObject

This clears up our Hierarchy and removes unwanted items from our memory.

Laser Objects being destroyed

One last thing I will mention here is that we have unlimited firepower and speed. We need to be able to control the speed at which we can fire our lasers. I will address this in my next article Creating a Cooldown System in Unity.

--

--

Marc Frappier

Poultry Scientist by profession, Computer Scientist at heart. After many years in the poultry business, I have decided to give my passion for technology a shot.