Day 8: Creating and Destroying GameObjects in Unity

In nearly every game, there’s always objects that must be created or removed as various actions occur. Enemies appear, items are collected, and all the cool visual effects must be created and destroyed as necessary. Here’s how we can do so in Unity.

Let’s make the player able to shoot “lasers”, which in this case are just yellow 2D capsule objects. First we’ll need to setup input to do that. Lets change the input manager settings to that the “space” key triggers the Fire1 input button. (This is under Edit -> Project Settings)

We can change other things here; removing “mouse 0” will prevent stray mouse clicks from firing lasers while we are working.

Now we’ll edit the Player script, to tell it what to do when Fire1 is pressed. We’ll have to create a new object while the game is running, so we need a variable to store that object, as well as calling a function to create it.

Add this at the top of the Player class
For those following along, I’ve moved all the movement code into its own method “CalculateMovement

Now the space key will create whatever object we want at the position of the player. Of course, _myLaser is empty by default, so we need to set it in the Inspector. Here, I made a 2D capsule object in the scene, dragged it into my assets to create a prefab, then dragged that prefab to my Player script to assign it to _myLaser.

If we were to play it now, we can create lasers by pressing space, but the lasers wouldn’t move. Let’s create a Laser script that causes them to move upward.

transform.Translate is another way to move objects, instead of modifying the position directly.

After attaching this script to the Laser prefab, all lasers that are created will now move upward. Click the play button, press space a bunch, and look at them go!

Looks like we’re done… but actually, not quite yet. There’s still a big problem here. Lets take a look at the Hierarchy after firing off all these lasers.

Even though the lasers move off of the screen, they still exist in the game world, and they will never be removed unless we do something. If we were to keep creating lasers without getting rid of them, the program would take up more and more of the computer’s memory and eventually crash.

Here we can modify the Update method of the Laser script, so they will be destroyed after moving upwards far enough. Lets play the game and see the result.

Now we can see in the Hierarchy that only a few lasers exist at any given time. The scene view shows that the lasers are removed soon after leaving the view of the camera. Thankfully, it’s that easy to clean up after ourselves when making a game in Unity!




I’m a passionate and talented software engineer seeking an opportunity in game development.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Install MySQL On Mac M1

Migrating from SmartThings to Home Assistant on the Synology DS220+

The Evolution of Maximum Availability Architecture (MAA)

Deep Dive into MongoDB Aggregation Framework — Part I


Why We Should Learn About Programming Especially Mobile Development ?

Multibinding in Koin (almost like in Dagger 2)

Asynchronous : What is Task.Run?

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
Blake Zoeckler

Blake Zoeckler

I’m a passionate and talented software engineer seeking an opportunity in game development.

More from Medium

Learn to code by making a 2D space shooter.

Mobile Game Progression

My journey becoming a Unity game developer: Main Menu: Main Menu Music and Start button activated

Unity Beginner — Day 02