Let there be… lasers! — Instantiate and Destroy GameObjects

Daniele Quero, PhD
6 min readJun 24, 2021

Objective: Understand the concepts of Prefabs and gameobject instantiation and destruction.

The next step in this game will be adding the possibility to shoot lasers. This simple act hides a variety of challenges and chances to learn a lot about Unity features.

We are going to use the so-called Prefabs. Every object in the game on the scene is a gameobject and till now we just created unique objects. But what if we need something repeated on the scene? Should we build a floor creating each and every tile as unique? Even if they are equal to each other? What if we want to reuse an object?

Here comes the Prefab. Prefabs are the blueprints of a gameobject: they store all the information about it and can be used to create (Instantiate) several, equal object on the scene. Let’s see how to create one.

We first create an object, for example, from the Hierarchy view, let’s say a 2D capsule. Let’s call it Laser. Then we create a new folder (just to keep everything tidy), called Prefab, inside the Assets folder. Now drag the Laser object in the Prefab folder.

In the Hierarchy view, the laser object turned light blue: that’s how you tell a Prefab from a unique object. But laser prefab also appeared in the folder! We can now delete the laser in Hierarchy and keep the one in the folder.

If we want to customize a prefab, always remember to do it from the prefab itself (Project view) and not from an instance (Hierarchy view). For example, let’s click on Laser prefab and manipulate the Inspector.

I changed the colour to red, added a Rigid Body 2D Component (click on Add Component and search) and a Capsule Collider 2D. The Rigid Body Component will allow to apply physics on the objects: set Gravity scale to 0, or the laser will fall along the y-axis. The Collider will set the bounds of the object, in order to detect collisions: check the box Is Trigger, in this way a collision could be used as trigger for events. All these changes, made from here, will be saved and applied to each instance of Laser.

Now the coding part! (I know you like it, and so do I). The objective is to instantiate a laser object on the scene, whenever we want during the game. Easy. In the Player Script, we declare a private GameObject (with SerializeField), _laserPrefab. Initialize it to null.

Now let’s write a simple method. We want first to check for Space key pressed (similar to the movement part). Input.GetKeyDown() is the way: this method will look for a specific key pressed (not necessarily hold, not necessarily released) and return a TRUE bool if pressed. We can tell which key using the KeyCode enum, which maps every key on keyboard and mouse making all very simple.

If the key is pressed, let the magic happen! In order to instantiate a prefab, we need to call Instantiate method. This method asks for:

  1. A gameobject reference, aka the object we want to instantiate
  2. A Vector3 representing the starting position of the new object
  3. A Quaternion indicating its rotation.

In this case, namely, _laserPrefab, the position of the player, no rotation. Call the method in Update().

But the variable is null, how can we access the inside of the if clause? Well, we override the value with SerializeField. If you look at the player inspector, The Player Script component has a new item, the Laser Prefab: simply drag the Laser Prefab (from Prefab folder) and drop it there.

Let’s have a look at the game, we expect to shoot laser when space bar is pressed. But…

They stay as idle as painted lasers upon a painted space!!

That’s because we didn’t say anything about laser behaviour. Let’s create a new Laser Script and attach it to laser prefab.

We want the laser pulses to simply go up along y-axis. As we already did for the player, we declare a speed variable and a Move() method to make the laser translate up.

Let’s have a look at the game. Cool! Lasers go BZZZZ all the way up, but there is another problem: The laser still exists outside the screen!

If you spam the space bar you will also overpopulate the scene!!

Here come the last thing we have to learn about instantiating object: how to destroy them! Back to the code, I will make it cool. Let’s grab the CameraBounds from inside the laser script. Also we declare an offset, just to be sure.

We can again use the camera visual to dynamically evaluate the bounds beyond which the laser should not go… and should be removed from game.

Since the lasers only travel along y-axis, if they reach and overcome the bound (corrected by an offset of choice) they are simply destroyed. To destroy an object, we use the static method GameObject.Destroy(), which asks for a game object reference.

Remember to use this.gameobject, to get the reference of the gameobject to which the laser script is attached. If you use just this, you will destroy the script… and the result would be just the end of motion!

Let’s have a look at the result.

Mission accomplished: the laser is removed!

--

--

Daniele Quero, PhD

A professional developer with passion for game developing and skill-growing. A former Nuclear Physics Researcher who changed his life to pursue his dreams