Instantiating and Destroying within Unity!

addam davis
5 min readMay 9, 2021

If you’ve been following along with my tutorials, then you will have just added input controls onto your player. That’s great, now you can physically make it move in a sporadic patter. Now let’s make your character shoot a laser. So, it can protect itself from the enemies we are inevitably going to make.

To start this process, we need to learn about Prefabs. Prefabs are a system in Unity that allow you to create, configure, and store a GameObject. Doing so will also keep the GameObjects components, property values, and child GameObjects as a reusable asset.

Knowing this we can create a projectile for our player. To do this go to GameObjects, 3D objects, and choose a capsule. I don’t know the kind of laser you were thinking but I think a little smaller would be a good idea. In the scale option on the transform component reset the three values to 0.2, also, rename the capsule ‘laser’.

To continue from here we need to make a Prefabs folder. Now grab the laser object from the hierarchy and drag it into the Prefabs folder. Once, you do that the name of the laser will turn blue. We should also create a material so we can have a nice color on our laser. Remember to name it laser_mat.

Now that we have it as a nice size and color, we can instantiate the laser. How do we instantiate in Unity? We can work through this using pseudocode. Say we want to fire the laser whenever we hit a key, let’s say the space key.

This is an if statement. An if statement is a true or false statement that we attach a reaction to.

Converting your pseudo code into the if statement we could write:

The Debug.log is a test. with this in place if you saved and went back to Unity and started the game. Every time you hit the space key a comment will appear on the console saying, “space key pressed” and that will prove that your if statement works.

We need to also create a private GameObject variable.

We are making great progress, now we need to save and go back into Unity. Select the player, and from the prefab folder drag the laser into the laser prefab on the script component. Now, the script has a reference and a holder for the object, and with that we can now instantiate!

Now replace the debug line with: Instantiate(name of the game object); if you labeled it that same as me the name is _LaserPrefab. So, it would be: Instantiate(_LaserPrefab);

If you were to go back to unity and run the game, every time you hit the space key you would spawn a laser on top of each other in the center of the field.

Now stick with me because I’m about to throw a few names at you that will make since once I’m finished. In Unity we handle rotation through Quaternion. Quaternions are how things are handled in terms of Oiler angles, and Oiler angles are the names for the X, Y, and Z, axis for a rotation.

Still with me? Great! Now, we want to spawn the object at the player position. The rotation we want to use is the default to the prefab, or no rotation.

let’s break this down. Instantiate i.e. spawn, the GameObject, the transform.position is in relation to the player, and Quaternion.Identity means default rotation. You will almost always want the default rotation.

Now if you run the game, you will spawn a lase where the player is. If this were a mine then I would say job well done, however, we are making laser fire. So, we want to add motion to the laser fire. If you remember back to “Simple Player Movements in Unity!” then you’ll remember how to add automatic movements to a GameObject.

First, we need to create a new script and remember, before clicking off the new script you need to rename it immediately. For the sake of this tutorial, I’m calling it the laser script. Attach the script to the prefab and open the script.

let’s build from our pseudocode.

great, but we need to set a variable for the speed.

Now run your game and attack the sky like it owes you money!

If you look at the hierarchy, you’ll notice a long list of clones piling in every time you hit the space bar. Not to worry we can handle those; all we must do is destroy them.

To do this we will need to start the game, fire once and pause the game. use the move tool to move that laser until it is just off screen and write down the Y axis for later, let’s say it was 8.

Open the laser script and start writing the pseudocode.

Now, every time the laser goes off screen it will be destroyed, and your hierarchy will stay nice and clean. Using this you can now instantiate prefabs, destroy GamObjects, and use if statements. Don’t be afraid to experiment and discover unique ways to use these functions.