Instantiating & Destroying Game Objects in Unity

Austin Hall
4 min readAug 30, 2022

--

In this game were using a laser to destroy objects/enemies. However, nothing exists in a game until we create it. So, first thing we need to do is create our laser.

Creating a primitive laser object

For our laser we are going to use a 3D capsule object. This will be replaced later with high fidelity graphics, but were not there yet. Right click in the hierarchy tab, scroll down to 3D object and select capsule. We set the position to (0, 0, 0) to start, and set the size to 0.2 x 0.2 x 0.2. I moved the object over so that it can be seen more clearly.

We are creating this object at runtime (when we start the game). Every time I hit the space bar I want the laser to fire. The concept here is reusable objects. In Unity we have something called Prefabs. Prefabs are nothing more than reusable objects that can me instantiated at runtime, and Ideally they hold references to each other. This allows us to create one object that can be reused instead of, say, creating 100 unique objects. Changing a single attribute on 100 unique objects requires 100 individual actions. with the Prefab, we change the attribute on the parent object and every object created subsequently from the prefab will inherit those attributes.

creating a Prefab

To create a prefab, we’ll first go o the Project tab and create a folder labeled Prefabs. when creating new folders or objects or anything in Unity, it is important to give them the correct name at creation. Trying to rename things after the fact is not impossible, but it is difficult.

Next we take our laser object and drag it into the Prefabs folder. if done correctly the object will turn blue. To create the laser we need to go to our player script and create a laser variable. After creating the variable, save changes and go back to Unity so it can compile.

Laser Prefab object variable created in Player script

then we need to attach a reference to that variable for the laser object. In Unity, Select the player object in the Hierarchy tab. In the inspector tab under the script section there will be a new item labeled laser prefab.

Adding the reference for the laser prefab variable in player script

From the Project tab, in the prefab folder, click and drag the Laser prefab into the box. Still in the player script, in the Update() method, we need to create the laser object when the Space bar is pressed.

Inside the Update() method within the Player script

Use the if statement and “Input.GetKeyDown(KeyCode.Space)” to get the input from the spacebar when pressed. the Instantiate() method will create the laser if the spacebar is pressed. _laserPrefab is the variable we created. transform.position tells Unity to create the laser at the player object’s current position. Quaternion.identity sets the rotation of the laser object to default, because we are not concerned with the rotation. %99 of the time we will use Quaternion. identity for rotation.

At this point, the a laser object will spawn at the player object’s current location each time the space bar is pressed, but it will not move, and all of the objects created will remain until we reset the game.

To fix our laser, we need to create a laser script like we did with the player object.

In the Project tab, right click on the scripts folder and create new C# script. Name is Laser. Click and drag that script onto the laser prefab in the in the prefab folder. That will cause Unity to assign that script to that object. double click on the script to open it in Visual Studio.

Laser script Variable creation

Create a private variable for speed.

Laser script Update() method

“transform.Translate” tells the laser object to move. Vector3.up tells the object to move in the up direction when it spawns. _speed is the variable for the speed of movement. Time.deltaTime converts to real time. the result: when the spacebar is pressed the laser object spawns at player location and moves in an upward direction at 8 meters per second.

Destroying object (in Update() method, Laser script)

Without some way to remove the object from the game, the laser pellet will continue to move up, outside of the game view, infinitely. To keep this from happening, I use the Destroy() method.

Create the ‘if’ statement. Use transform.position.y to find the laser’s vertical position. When that position exceeds 8.0f( upper boundary of game window), call Destroy(this.gameObject). We can use this.gameObject because we are in the laser script and want to destroy the laser object itself.

--

--

No responses yet