Unity3D: Instantiating & Destroying Gameobjects
How to instantiate and destroy lasers within a 2D Space Shooter game within Unity.
Objective: Our 2D Space Shooter should spawn lasers from the player and destroy the lasers when they leave the screen.
Note: If you want to see the previous 2D Space Shooter article, please click here.
The first thing to create a laser. Create a capsule in the Hierarchy.
Create a folder under Assets called Prefabs.
Drag the Laser game object into the Prefabs folder.
You can delete the Laser game object from the Hierarchy. It is now a prefab, meaning it can be duplicated with all of its settings, scripts and applied materials remaining the same.
With our laser game object made, we want it to “spawn” when we press the arrow key. We will be going back into our Player script to spawn the lasers. Here is what we will be doing.
- Call out a new variable which the laser prefab can be assigned.
- Create an if statement that will spawn the laser prefab when the space key is pressed.
We will create a private game object that will be private, but can be assigned our prefab within Unity.
Next, our if statement will reflect the following logic: if the space key is pressed, spawn the _laserPrefab.
The if statement will be written within our void Update class for now. According to the above logic, it will appear in code as:
The space key is called out within the if statement as Input.GetKeyDown(KeyCode.Space), which can be translated as the input received when the key is pressed down —that key being the space key.
To spawn, we will use the Instantiate() command. According to doc.unity3d.com, Instantiate() Clones the object original
and returns the clone. It is written in the following structure:
Instantiate(Object original, Vector3 position, Quaternion rotation);
Our Object original will be the game object _laserPrefab created in our script.
The Vector3 position will be our current position, or transform.position.
Quaternion rotation refers to the orientation of the part in 3D space. This will be in our Instantiate() written as Quaternion.identity.
All together, our Instantiate() will be written as:
This will be written within our if statement.
With our script saved, we need to assign our Laser game object in the Prefab folder in our Project to the _laserPrefab game object in our Player script that is assigned to our Player.
Next, we need to write a script that for our Laser’s behavior when spawned. It needs to (1) fly forward and (2) destroy its cloned self when it leaves the screen.
Right click on the Scripts folder and make a new C# script labeled Laser.
In the new script, we will create a variable for the laser’s speed. It will be a float value so the speed can be fine-tuned.
Next, we will tell the laser to move, or translate, up the screen at _laserSpeed.
With the Laser script saved, we will drag it onto our Laser prefab in Unity. When the game is run, we will see the the lasers shooting up the screen when we press the Space key.
Notice that more laser clones are generated within the hierarchy. These clones need to be deleted to avoid cluttering our game with thousands of game objects.
An if statement will be created saying the following: if the position of the laser exceeds the screen’s edge, destroy it.
Within the same update class, we will write the code as follows:
When the game runs, the lasers will shoot when the space key is pressed and the laser game objects will disappear when they leave our screen.
Success! Tutorial completed! Please make sure to visit my Medium page to see other tutorials regarding C# and Unity3D.