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.

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.

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

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.

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.

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.

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.

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.