Unity: Adding and Removing Objects

Photo by Salman Hossain (Unsplash)

Unless you’re creating a scene that has the user experiencing depths of darkness from within a black hole, you are going to want to add objects to your scene. You’ll also — most likely — want to remove objects. Let’s find out how.

Let’s suppose we are creating a 2D arcade shooter. We are firing laser bullets that should appear as if they are leaving our ship’s gun when the space bar is pressed. For this behavior, we will make use of Unity’s Instantiate() method.

The Instantiate() method takes a few different parameters, but we are only concerned with a few for now:

  1. Instantiate first wants to know what object it should add. We are shooting our gun, which is a part of our Player behavior. As a result, in our Player script, we set up a game object container that will act as a handhold for us to grab our laser object* (private GameObject _laserPrefab;
  2. )so we can assign the correct Prefab object to the behavior. Makes sense, right? Instantiate(_laserPrefab,
  3. With the second parameter, we’ll want to indicate where to add the object transform.position + new Vector3(0, 1.05f, 0),
  4. Finally, we will define how the object is rotated in space Quaternion.identity);

All together now: Take a Laser bullet, add it to this position, and keep the default rotation. Nice and easy.

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

However, we’ve now shot a laser that will seemingly go on forever, as nothing is instructing the object otherwise. The behavior we want is to make sure the laser object gets destroyed when it is out of bounds, so that each instance (bullet fired) doesn’t remain floating around in memory, hoarding precious resources, and cluttering up the hierarchy.

Much like life, destroying an object in Unity is pretty straight forward. Remember that this.gameObject refers to the object that the script is attached to. In our 2D Shooter example, once fired, the laser begins moving up at some defined speed. Logically, it doesn’t make sense to delegate the bullet clean up to another object. That is just plain rude. Therefore, the rogue laser will need to clean itself up and self-destruct from within the Laser script. Note: The Laser script is attached to the Laser object (prefab). Hopefully it is clear now that a call to this.gameObject from within the Laser script would destroy the Laser object.

Hopefully it’s not too anti-climatic, but here is the line of code in all it’s glory:


*Bonus Tip! Make sure to add a [SerializeField] reference above your GameObject variable. This will allow you to easily associate your objects with their appropriate scripts from within the Unity editor. For our Laser object the code would look like this:
private GameObject _laserPrefab;



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store