Instantiating/Destroying GameObjects in Unity

Thomas Mauro
4 min readMay 7, 2022

--

At this point you have your spaceship or gun, now it requires a projectile. We will call the Cube Player (basically the spaceship) and the Capsule is Laser. We want the Laser to project from the Player, but what happens after the Laser is fired? It goes on forever. There's where destroying the GameObject comes in otherwise you'd have infinite Lasers just staying in the game for no reason. You can Destroy the GameObject at a certain location point when it is off of the screen or after a specified amount of time has passed.

— — — — — — — — — — — — — — — — — — — — — — — — — — — —

Then bring in the Capsule (Laser). Scale it down to a good laser beam or bullet size.

Get a good idea or a precise idea of where you need an object to instantiate you can drag the Prefab into the Scene View, place it exactly where you want it to go, and grab the Transform info from there.

— — — — — — — — — — — — — — — — — — — — — — — — — — — —

PREFAB:

A Prefab is pretty much a clone of a GameObject. Once it is cloned into a PreFab you won't have to make a new one all the time. You definitely want/need to Prefab something like a Laser Beam or Bullet as there will be used a lot as projectiles for many projectile devices you may have in your game. Here will make the Laser a PreFab so it can be used for the Player. First, you need to make a Folder called Prefabs then drag and drop your Laser in the Hierarchy to the Prefabs folder. From the Prefabs folder, you can now access the Laser Prefab to drag into the Player Prefab section on Unity Inspector. The example below will show you how to make the prefab. The existing Laser I had will demonstrate what it is supposed to look like once the Laser has the script onto it and you have resized/colored it. Once a Prefab is made you can delete the object in the hierarchy as you will now access the object via the Prefabs Folder.

— — — — — — — — — — — — — — — — — — — — — — — — — — — —

Okay so how can the object spawn into the game? That is called Instantiate in Unity. Instantiating will clone an original object and returns it back to Unity when done.

You need to spawn/clone/use the Laser from a starting point. So when you press the shoot button it has point it spawns into Unity to begin its process (projectile).

The best place to start will be the Player script. In this example, our Player is the Cube itself no arms no gun just the Cube. So the Laser needs to come from the Cube. We don't want it coming from inside the Cube but rather from right outside the top of it. Reminder; get the exact transform info

So it is set perfectly to Instantiate right above the Player. I will set the speed very slow at first to demonstrate.

— — — — — — — — — — — — — — — — — — — — — — — — — — — —

Destroy:

Now the Laser needs to be destroyed so it does just stay there filling up the Hierarchy forever.

I have deactivated the code to destroy the Laser. Now it will just fill up the Hierarchy forever until fixed. This is why you must Destroy GameObject.

In a quick nutshell that is Instantiate and Destroy GameObjects in Unity. Don't forget about Unity Documentation.

--

--