Dust to Dust — Creating and Destroying Objects in Unity
Videogames are complicated, filled with different textures, meshes, scripts, UI, and more. It is unthinkable it have all of that data permanently loaded. Fortunately, it is common to utilize instantiation to bypass some of that strain on the system!
First thing, instantiate is synonymous with create. It is just the verbiage that Unity understands. When you instantiate, you are creating an object in the applicable scene. Usually, this instantiation comes from a prefab.
A prefab is a prefabricated asset that is used over and over again. Anything that you will be using over and over again should be a prefab!
So we can build one laser asset, then instantiate it when we want to fire our weapon, saving resources!
Let’s make a simple laser placeholder in Unity first.
Shrink the capsule down to a more believable size, and then we can prefab it.
Now in the Project folder, make a new folder called “Prefabs”.
Rename your capsule and drag it into the Prefabs folder.
Immediately you should notice that a blue cube icon appears next to it in the Project Folder, Hierarchy, and even the Inspector.
Now you can make any needed edits to the laser, save the changes, and all lasers will have those changes.
Add a new C# script to the laser and open up the script for editing.
We can reuse much of the logic from the player to achieve our desired goals with the laser itself. We want the laser to:
- Be instantiated when we press the spacebar.
- Go up the screen at a set speed.
- Destroy itself once it goes out of the visible game area.
The instantiation will be implemented on the player script, so we will do that one last.
First, let’s set up a speed variable, the same as we did for the player.
Next, I create a new method called “LaserMovement”. You may name this to your liking as well.
Inside this method will be the logic for the laser to move up on the screen. We can implement it as follows:
Next, add an “if” statement to destroy the object if it goes off screen. In my case, the laser is offscreen at the Y value of 8.
The entire function should look something like this.
NOTE: Make sure to add the function to Update or it well never be called!
That’s all for the laser script, save your changes to your prefab and let’s move over the the player script and set up the firing mechanic.
First off, the player script needs to know what the laser is. So we make a new variable of type gameobject to hold that reference.
Heading back to Unity, we can drag our prefab into the new slot for laser on the player.
Now the we can instantiate the laser from our player script!
In the player script, I made a new function called FireLaser.
Using an “if” statement, we can set the parameter to be when we press a specific key (in this case, the spacebar).
Finally, inside of this “if” statement, we want to instantiate our laser and set it’s location.
Transform position means the current position of the player (in this case), and we add a new Vector3 to offset the laser to look like it is coming out of the front of the player model. Finally, Quaternion.identity means keep the current rotation of the object the same.
Add FireLaser() to your update function and test it out!