Instantiating and destroying game objects in Unity.

Simon Leen
4 min readMay 15, 2021

--

Objective: Instantiate Laser objects from the Player and destroy the objects after they leave the screen.

Now that our Player has some basic movement, we’ll start with getting them to shoot lasers. To do this we need to create a laser prefab and then set the logic(script) to allow the Player to shoot using user input. Each time the Player shoots a laser, a new laser prefab will instantiate from a position we set just infront of the Player.

To create the laser, start by right-clicking in the hierarchy, go to 3D Object and select Capsule. Reset the Capsule position in the inspector to 0,0,0 then scale down to 0.2 and rename the Capsule, Laser.

Create the Capsule(Laser)
Set position and scale

Next we want to create a prefab of the Laser. Prefabs are just reusable objects. In the project window, create a Prefabs folder inside the Assets folder, then drag the Laser object from the hierarchy into this new folder. Prefabs allow one object to become a shared object. This means in a scene where you have multiple versions of lets say a Laser, you don’t have to pull in multiple laser objects just one that gets used over and over again.

This also makes editing or updating the objects easier and faster. Rather that updating multiple versions of the laser, you would only update one and save the changes to the prefab, which would save the same changes to all the other copies of the prefab. You have to set the override to push the changes to the other objects which allows you to make changes to individual objects without changing the others.

Next for the scene and visual aspect, create a new material in the Materials folder within the Assets called Laser_mat and change the color to something you want to use. Drag the material onto the Laser prefab or the Laser game object, just remember to apply to override to the prefab.

To allow the Player access to the Laser to shoot, set up a GameObject in the Player script. Set it as private to control access to it but set it as a serializable field so that you can set the Laser prefab in the inspector.

Laser prefab object in Player script
Assign Laser prefab to script in inspector

Next we create the instantiation behavior within the Player script. Call Instatiate with the laser prefab, set the position to the transform position(Player) and set the Quaternion to identity(set the rotation of the instantiating object to the default rotation).

Instantiate laser on space key input on the players position
Laser being instantiated under player when space key in pressed

To set the laser to instantiate just infront of the player rather than under it we add a new vector3 to the transform position adding 0 to x and z, and 1 to y.

Moving the laser just infront of player on instantiation

To get the laser moving, create a new Laser script and drag it onto the laser prefab. Open the script and set a laser speed private float and make it serializable. Set the speed you want the laser to move.

Set laser speed

Now in the Update method of the Laser script, set the laser to move(translate) its position upwards using deltaTime(1 meter/second) by the speed you set for the laser.

Tell the laser to move up at the set speed per second
Laser shoots forward from in front of player

Now that the player can shoot lasers, the next thing is to destroy the laser objects after they pass out of the screen view. For this we need to check if the laser object has passed a certain point on the Y-axis and if it has destroy the object. For an example we will set the destroy point between two new cubes to verify it works. Create two cubes placed just below the top center and leave a small space in the middle for where the laser will destroy(sorry no effects yet, it will just disappear).

Check if laser position passes 6 on the y-axis and destroy if it has
Lasers being destroyed as they reach the target height

Now that it works, remove the two cubes and set the destroy point passed the screen height allowing a small clearance for later changes to visuals etc.

For the next post, a laser shooting cooldown will be put in place to stop the player constantly being able to shoot and forcing a minimal time between new lasers being instantiated even if the space(fire) key has been pressed.

--

--

No responses yet