Fire Everything!

Adding objects to your project in the Unity Editor is all well and good, but what if you could add objects to your scene while the game is running? In game development, creating objects is called instantiation and you can accomplish this in your C# scripts. By using instantiation, you can create many game features — including the ability to shoot lasers! Let’s step through how to do this.

Create a Prefab

Before you can instantiate an object through code, you have to design the object you want to instantiate.

Create a Game Object

First, you’ll add your object in the Unity Editor like normal — setting the size and name, adding components as needed.

Adding a capsule object in Unity Editor to represent a single laser shot

Make the Game Object a Prefab

Now that you’ve designed the object, you need to make it a Prefab, which just stands for Prefabricated — or something you created ahead of time.

Add a new Prefabs folder to your project

First, in the Project section, create a new folder within your Assets folder and name it Prefabs.

Add the Game Object to the Prefabs folder to make it a Prefab

Then, drag the Laser object you created to your Prefabs folder. This will turn it into a Prefab, which is signified by the blue box icon in front of the object name.

Create multiple versions of the same exact object using Prefabs

Now that your object is a Prefab, you can easily drag multiple versions (or instances) of the object into your scene. They will all be added to the scene at the same position, since the Transform component is identical for all instances.

Overriding Prefabs

Prefabs make it easy to add multiple versions of the same object to your project. But what if you need to change something? You could go into each instance of the Prefab to make the change, but you’ve got better things to do. Enter Overrides.

Using Overrides to change the material for a prefab

Instead of making the change to every instance of the prefab, you can make the change to one instance and apply to every instance of the prefab, as well as any new ones you add in the future. After making the change, click on the Overrides dropdown in the Inspector to see what changes you made and click Apply All. This will update the Prefab itself, as well as all instances in your project.

Now that you’ve designed your laser object, you can remove any instances of the laser from your scene.

Instantiate the Object

In Unity, you can use the Instantiate() method to create an object through code.

First, identify the script you want to call this method from. Be sure to pick a script that is not attached to the object you want to create. In Unity, a script is attached to an object and defines the behavior for that object, but the script does not exist until the object does.

Create a variable to hold the reference to the prefab

Since the laser is being fired by the Player, you can call the Instantiate() method from the Player script. The Instantiate() needs to know what object you want to create, so you’ll need to create a reference to the prefab. At the top of your class, add a GameObject variable to hold the reference and add the SerializeField attribute so you can assign the Prefab from the Inspector.

Assign the prefab to the reference variable

To assign the prefab in the Inspector, save your script and return to Unity. Once it compiles, select the Player object. In the Inspector, there will be a field in the Script component for the prefab reference. Click and drag the Laser prefab from the Prefabs folder to this field to assign it.

Then, in Update() call the Instantiate() method, passing in the reference to prefab, a position and a rotation. Since the lasers will originate from the Player, you can use the Player position as the laser position. To pass in rotation, you’ll use a Quaternion, which is just the structure Unity uses to represent and communicate rotation data, like how a coordinate can be represented as (x, y). Similar to using Vector3.zero for (0, 0, 0), you can use Quaternion.identity to pass in no rotation.

Now, a Laser object is created every frame at the Player’s position. With 60 frames per second, the individual capsule objects appear as a continuous line — obviously there is still room for improvement.

By wrapping the instantiation in an if-statement that looks for the Space Key to be pressed and adding a script to the laser prefab with Transform.Translate in the Update() method, you can create laser firing functionality in your project.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Lock Free Queues

First Steps into Software Engineering — Internship Journey at Jublia

KLAY-KDUCATO pair newly added on Turtle King Finance Pool

Outsourcing vs in-house development

Text Recognition in Flutter Using Firebase’s ML Kit

Integrating Enjin Coin (ENJ) with Unity using the Enjin Unity SDK — Part 1 — Enjin Account & Wallet

Code Without Tears

I’ve Cracked the Code, Please Call Tech Support- Episode 13, Drop-Shadows of the Empire

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Erin Brown

Erin Brown

More from Medium

Spawn Manager Cleanup

Indy Pro ’22: Talent Training

From Prototype to Work of Art

If You Like Minecraft, You’ll Like Valheim, and Here’s Why