Unity Guide

Instantiating & destroying gameobjects in Unity

A quick guide to instantiate and destroy gameobjects in Unity for your game

Fernando Alcantara Santana
5 min readMay 6, 2021

Objective: Instantiate and destroy gameobjects in a space shooter game using Unity.

Create the laser

To continue with the space shooter game style, the player needs to generate lasers or any type of ammo to destroy enemies while avoiding collisions in the space. In order to do it, we first need to create the gameobject for the laser:

A 3D capsule can be used to represent the laser bullet.

Once we have the gameobject and edited its properties we can check it in the game view or the scene view to verify if it’s appropiate:

The 3D capsule was resized to make it look like a laser bullet.

Then, if you want, create a new material to easily identify the laser bullet in the scene:

Drag the material into the laser gameobject and the material will be attached to it:

Now the laser looks red as it’s the color of the material

Then, we need to create a new script and attach it as a component in the laser gameobject:

Once the script is a attached you’ll see it in the inspector.

Next, create a folder for the prefabs and drag the laser gameobject from the scene to the folder to save it as a new prefab. If you want, remove the laser from the scene as it’s a prefab now and we’ll instantiate it from the player script to shoot.

Laser Behavior

Now open the laser script to modify its behavior in the scene. First, we need to have a variable for the laser speed and another for the limit of the space that the laser will travel to:

As the laser is intended to go up we only need a limit coordinate for the Y axis.

Then, in the Update function, we can assign the movement of the laser whenever it’s instantiated in the scene. For this space shooter, the laser will move towards the north:

Using the Translate function like this will assign the direction and speed per second of the laser.

Destroy the laser

Now to destroy the laser gameobject whenever it reaches the upper limit of the space we need to add the next condition:

If the laser goes beyond the Y axis limit gets destroyed.

If we save the script and execute the scene in the Unity editor we’ll see that the laser behavior is to go up and then get destroyed if it reaches the upper limit:

Instantiate the Laser

Now, to instantiate the laser when the player shoots in the game, we need to add a variable inside the player script (attached to it) to have a reference of the laser prefab:

[SerializeField] will allow us to drag the prefab in the Unity inspector as the variable is private

Then, select the player in the scene and you’ll see the new variable for the laser prefab:

Drag the laser prefab from the prefabs folder into the script component of the player:

Return to the player script and create a new function to instantiate a laser every time it’s executed. The Instantiate function allows us to create a copy of the laser prefab in the position and rotation we choose.

To avoid creating the laser in the player’s position I added a vector to create it above the player.

Now, to instantiate the laser every time the player shoots, we need to add an input event trigger in the Update function that calls the ShootLaser function when the spacebar of the keyboard is pressed:

Now, if we execute the game in Unity, we’ll see that every time we press the spacebar a clone of the laser prefab is added to the scene and gets destroyed once it reaches the upper limit of the scene:

And that’s it, you can instantiate and destroy gameobjects with Unity! :D. I’ll see you in the next post, where I’ll be showing how to create a cooldown system for the space shooter in Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Fernando Alcantara Santana

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).