Instantiating & Destroying Gameobjects in Unity
In our quest to develop our game prototype for GameDevHQ, we’re going to want to know how to instantiate and destroy gameobjects in Unity. If you’ve been reading along, or are already familiar with Unity/C# terminology, then you’ll know that a gameobject is anything we add to the game. In this case, we can explore these terms using a 3D cylinder shape named “Laser”.
The Laser is the element that we’ll fire from our Player character during gameplay. Since we’ll need a bunch of lasers, we’ll create a Prefabs folder in our Assets Project hierarchy. This will allow us to clone the lasers and even add special features to individual assets, if necessary. We will drag our created laser from the Game Hierarchy to the Prefabs folder, creating a copy within.
Now that we have that set up, we can look at the C# script necessary to perform our intended actions. Let’s take a look at instantiate. To instantiate a gameobject means “to create” or “spawn” the object. For our purpose, the spacebar key will trigger spawning our Laser. First, we’ll define the Laser Prefab GameObject . In the above screenshot, we’ve named it _laserPrefab.
Nest, we’ll want to link the pressing of the spacebar key down with instantiating a new Laser object. One of the first things I did was ensure that Unity was recognizing the key press by allowing the console to return a message to tell me it recognized the action using the “Debug.Log” command. After saving and running, I was able to get the Console to display my chosen message in the bottom of my Unity screen. Double-clicking on the message brought up a Console window, which I’ve now docked beside my Game window for easier access.
Now that I’m sure communication is fine, and I now have a visible record of the goings-on with my gameobjects, I can focus on the instantiation code. After commenting out the Debug.Log, we can now Instantiate our defined _laserPrefab gameobject, its position and its rotation. We’ll do this by typing the following:
Instantiate(_laserPrefab, transform.position, Quaternion.identity);Get Stephanie Wells’s stories in your inbox
Join Medium for free to get updates from this writer.
Now, once we save and compile within Unity, we’ll want to link our Laser Prefab to the Player. We’ll do that by first deleting the Laser in the Game Hierarchy, and then dragging our Laser located in the Prefabs folder into the Laser Prefab section under “Player (Script)” This lets Unity know what object we want to spawn when the spacebar is pressed.
Now, when we compile and run our game, we’ll see our Player shooting lasers. They won’t go anywhere, but if you’re new to this, it’s still pretty amazing to see.
We want our Laser to do a little more than just spawn on the screen. To keep our code clean, organized and logical, we’ll create a new C# script called Laser to hold the various details of this object. We’ll want to define the speed and direction of the Laser within this script, and then apply it to our prefab object.
Our speed should be set to a float variable to make it easier to adjust/fine-tune later. We’ll use the transform.Translate statement to send our laser up, multiplied by our defined speed, multiplied by our real-time call.
Now, our Laser clones are happily firing upwards when we press the spacebar, following our Player as it moves across the gamefield. If you notice in the Game Hierarchy, those clones are sticking around. We can clean that up with the Destroy command. When I first heard this term, I was kind of stoked thinking we would make some explosions. However, “Destroy” in this case, means simply cleaning up our objects so that they’re not in the way. We’ll save explosions for another day.
First, we’ll decide where the Laser object should be destroyed. We’ll use the game walls itself to determine how high up our Laser needs to travel before it is no longer seen on the screen. This “y” value will determine when the Destroy takes place. For my screen, a value of “8” was sufficient. I made note of that within my Laser pseudo code for planning. Now, we’ll use an if statement to bring this all together. If our Laser has reached our given height, the built-in Destroy script will erase the object from existence. I used a “this” before my gameObject to tie everything together.
Now, after saving, compiling and running, we can see that the Lasers clean up and disappear after reaching the top of the screen. Our prototype is starting to look like a true retro game, and with only a few lines of code!






