Instantiating & Destroying Gameobjects in Unity

This article will look at creating or instantiating a gameobject and then destroying it so the game does not become cluttered with copies. To show this, we will create a shooting system that will spawn a prefab of a laser. The Laser will then travel upward, and after it has gone off so far off the screen, it will be destroyed.

Final Example

Let’s begin with creating the Laser. Create a Capsule that will act as the Laser, then name it “Laser” and make it 0.25 on all Scale values. At this point, you can create a material for the Laser so you can see it easily, but it is not necessary.

Creating the Laser Object

Now to turn the Laser into a Prefab. A Prefab is just a way to make a gameobject that you can use across the project with the default set values and components you give. Making a Prefab is easy. You drag the gameobject that you want to turn into a Prefab into the Assets folder, but it is best to stay organized and have a folder in the Assets folder just for Prefabs. After you have made the Laser a Prefab, you can delete it from the scene.

Making the Laser a Prefab

Now we can start making the code to spawn the Laser. I am going to put this code on the Player since that is what is firing the Lasers. First, you need to create a variable that will represent the Laser Prefab that we will spawn. Next, in void Update, make an if statement looking for the space key to be pressed. Then inside the if statement, we will use Instantiate to spawn or create a new laserPrefab at the position of the Player with transform.position and at the same rotation values as the Prefab with Quaternion.identity.

Code to Spawn the Laser with the Space bar

Now back in Unity, select the Player and drag the Laser Prefab into the Laser Prefab gameobject field and then you can enter Play Mode and fire away.

Lasers spawning on Key Press

Now that the Laser is spawning, let’s get it moving. To do that, create a new script named Laser and attach it to the Laser Prefab.

Creating the Laser Script and putting it on the Prefab

Now in the Laser script, we will program it to move. First, make a public float named speed with a value of 8. Then, in the void Update, we want to adjust the Laser’s transform every frame, moving upward. We will use Translate and move it by Vector3.up, which is the same as new Vector3(0, 1, 0), then multiply it by the speed variable and multiply that by Time.deltaTime.

Code to make the Laser move

Now the Lasers will fly upward and off the screen, but they begin to clutter the Hierarchy quickly, as you can see.

Pew Pew

With the Lasers moving, let’s clean them up after they go so far off the screen, so they don’t clutter up the scene. In the Laser script, inside of void Update, after the movement code, add an if statement that checks it the Laser’s y position is greater than 8, which will put it off the screen. Inside the if statement, have it Destroy the Laser gameobject once it exceeds 8 on the y.

Code to have the Lasers Destroy them selves

And now the Lasers will destroy themselves after they go past 8 on the y axis.

Lasers Destroy them selves

And there is a simple example of how to instantiate and destroy gameobjects in Unity. In the following article, we will expand the firing system to have a cool-down.

Unity Developer, Software Engineer, Game Designer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store