Instantiating & destroying game objects in Unity

Daniel Kirwan
4 min readMar 26, 2021

--

When creating games in Unity, you will at some time get to the point where you will need to instantiate a game object, well what does that mean? It means to create an instance of an already defined game object at run time. This way we can create multiple instances of the object without having to create multiple game assets in our project.

For this tutorial, we will be continuing with the project I’m currently working which is a 2D space shooter. So far we have a cube as a player and I have created a 3D capsule as a laser.

Instantiation

To be able to instantiate a game object in Unity you will need to create a prefab of the object in your project. You do this by dragging the object from the hierarchy into the project window, ideally into a prefabs folder.

If you’re using Unity 2019 the square and text of the game object will turn a light blue colour. This means we can now instantiate the laser while the game is running.

I would like these lasers to come out of the player when they press the space bar. I have already set this up in the player class along with a reference to the Laser prefab.

Above you can see the implementation of the space bar press that is calling a method called SpawnLaser.

In the SpawnLaser method, I am using the Unity Instantiate method and it needs 3 parameters to work. The game object to be spawned, the position to spawn, and the rotation to spawn at. I am choosing to rotate my lasers on the z-axis because I want my lasers to fire from left to right. This gave me a slight problem when I came to tell the lasers in which direction to use.

You can see that my lasers are not moving along the x-axis as I want to, and that is because I have rotated the object and the x-axis is now pointing in a different direction. I made a temporary fix by changing the movement from the x-axis to the y-axis but I wanted the laser to fire along the x-axis.

In the update method image, you can see the implementations that I tried at first and while the second transform.Translate works I wanted to find another solution.

After some research into the Translate method, you can add another parameter to the method. If this optional parameter is left out then it defaults to using the game objects local axes for directions. The other option is to add Space.World and this makes it so that the game object moves relative to the coordinates system in the game world.

Now my lasers are firing in the right direction which is along the x-axis and not the y-axis.

There are other ways to fix this issue, I can have added a child object to the laser and added a sprite for the laser. The main laser would not be rotated but the child sprite would be rotated and the effect would be the same but I like the solution I’ve gone with for now.

Destroying

Destroying game objects in Unity couldn’t be any easier. You call a premade Unity method called Destroy(yourGameObject);

All you need to do is pass in the game object name that you want to be destroyed and voila it will be destroyed. You can also set a time for the object to be destroyed like so, Destroy(laser, 2f). This destroys the game object named laser in 2 seconds. Using the time parameter is completely optional and up to you on when to use it.

For the project I’m working on I have set up a shredder game object with a Destroy script on it. If the game object being collided with is a laser then it will be destroyed. More on collisions in a later article.

You can see the shredder game object running below.

And that's it for this article on instantiating and destroying game objects in Unity.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet