Lighting Bolt…Lighting Bolt…Lighting Bolt — Instantiation & Destruction “Magic”.

Games Woods
4 min readApr 29, 2021

One of the many cool aspects of Unity is its ability to create what’s known as Prefabs. These are prefabricated Game Objects that you can create once. Assign all its components, property values and have them available as reusable game assets.

This is perfect when you want to instantiate a GameObject as the game is running by say player input!?!….for something like say a Laser Bolt!!

Pew Pew Pew!

We’ll start off by creating the primitive Object…Behold the Mighty Pill Laser!!

To create it, go to your hierarchy pane, right click → 3D Object → Capsule. Rename it in the inspector as Laser. Reset the position to 0, 0 ,0. And adjust its size to 0.2, 0.2, 0.2

Next we want to give this laser a color, so let’s create a new material for this prefab. Right click your Materials Folder → Create → Material.

Change the Albedo color to something that feel laser-y to you. I choose Purple. Rename the material to Laser_mat in the inspector and then drag it onto your laser pill.

Now that we have all the elements of our Laser setup we need to make it into a Prefab. That is as easy as dragging it from the hierarchy window into the Prefab Folder you created in the Project window.

Here’s an important thing to remember if you happen to be changing attributes of a Prefab in your Game Scene and you want those to be permanent you must remember to click on the override drop down in the inspector and “Apply All” for those changes to be applied to your Prefab.

Now that we’ve created the Laser Prefab we want to be able to instantiate it when the player presses the space key.

In our Player script we need to create a variable for the Laser Prefab, it should be a private object and be preceded by a [SerializedField] so that we can access and assign the Prefab it in the inspector. Save your script and jump back to Unity.

Click on your player in the hierarchy window and grab the Laser Prefab from the Project Window and drag it into the empty Laser Prefab box under the player script section.

Great so now we player script has access to the Laser prefab. Let’s instantiate it when the spacebar is pressed.

FIRE!

In the above statement we are telling the player script that if the spacekey is pressed down, to instantiate the Laser Prefab from the players current position (transform.position) and that it needs no rotation (quaternion.identity).Save your script and checkout the laser get deposited …in the …same….place.

No worries we have to get these lasers to move and to Destroy themselves when they go off screen. We do this by creating a new Script called Laser and attaching it to our prefab. Now jump into the new script and lets give the laser a SerializedField _laserSpeed variable (I started low to catch the laser and adjust from there)

In the Update Method similar to how we moved our player we apply the transform.Translate(Vector3.up) times our Laser Speed and times Time.deltaTime. The only different from player movement here is that we already took care of the keypress when we instantiate the laser in the Player script.

Finally through a little google search I figure out how easy it was to destroy the laser by using an if statement with the conditions that if it reaches a certain y axis position/value on the scene we simply call on Destroy(this.gameObject). While experimenting with Destroying the laster I tried Destroy(this) instead of the above, which destroyed the Laser.cs script and left me with a ceiling of purple lasers. Easy bug fix that was :-)

This is a Bug!

After fixing that bug we get the expected and desired behaviour of some sweet sweet laser bolts firing off and destroying themselves when they missed their mark….silly laser bolts…if only you had something to hit…Hmmmm.

--

--