Creating Objects and Getting Rid of Them in Unity

Christian Carter
4 min readMay 11, 2021

--

Creating Objects and getting rid of them in Unity are common actions. In order to create an object you will need to instantiate it. To get rid of an object you will need to destroy it. But, before you can instantiate or destroy an object first you need to have an object and for that we need to create a prefab.

For this example you are going to create a laser. First create a Capsule and change its name to laser. Set the X, Y, and Z scale each to 0.2. Add a material to it. Now create a Prefab folder in your Assets. Then, drag the laser down into the prefab folder. Now delete the original laser capsule you created. Create a C# script called Laser. Add that to your prefab laser.

Now you need to go into your Player script to add the code to instantiate or create the laser in the game. First you need to create a variable to reference the Laser prefab. The variable can be set up like this:

[SerializeField]
private GameObject _laserPrefab;

Now in the Unity editor in the Player you can drag and drop the Laser prefab into the Laser Prefab variable spot in the Player script. Now in the update script you can add code to instantiate a laser every time the space bar is pressed. In the Update function add the following code:

if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(_laserPrefab, transform.position, Quaternion.identity);
}

First, in the if statement you use Input to know when the space bar is pressed. When it is pressed it will call the Instantiate function. The first thing we pass to this function is our laser prefab variable. Next, the position of the player since that is where the laser will be created. Last, the Quaternion.identity tells the laser which direction or angle it will be pointing. Now when you run the game it will create lasers as you move the Player about when you press the space bar.

Now you need to make the laser move up towards the top of the screen. To do this you can use the transform.Translate function. Go to your laser script. Before you add the translate function you should add a speed variable and set it to 8.0f. Now add the translate function.

transform.Translate(Vector3.up * _speed * Time.deltaTime);

The Vector3.up will make the laser go up one meter per frame. Then multiplied by the speed variable will make it go at eight meters per frame. Multiplying by Time.deltaTime, the time between each frame, will reduce the speed so it only move at eight meters per second.

Next, you need to get rid of or destroy the lasers once they have moved off screen, that way you don’t end up with hundreds of lasers flying off into space forever. First, you need to setup to setup an if statement to check for when the laser is off of the screen. You can use transform.position.y to find the vertical position of the laser. For this example you can check if it is at 8.0f which will be off the screen. If you want to check you can watch the position of a laser until it leaves the screen and verify what the y is at that point in time. To destroy an object you use the Destroy function and the pass it a gameObject. Since you want the laser to destroy itself you can pass this.gameObject which refers to itself. Here is the code you can add in the Update function.

if (transform.position.y >= 8.0f)
{
Destroy(this.gameObject);
}

With this done you can now watch in the Hierarchy view after you create a laser, after it has moved off the screen it will be destroyed and removed from the Hierarchy.

One last tip when creating something like the laser is you may not want it to be instantiated right in the middle of the player. You can offset it by adding an addition vector to the position like in the code below.

Instantiate(_laserPrefab, transform.position + 
new Vector3(0, 0.8f, 0), Quaternion.identity);

Now the laser will instantiate just in front of the player!

Good Luck Adventurer!

-Chris

--

--

No responses yet