Instantiating & Destroying Gameobjects in Unity

Check out the image below. Look at all those lasers! Believe it or not, we just need to make one laser object, then we can spawn as many clones of it as we want!

PEW! PEW! PEW!

It’s super easy:

“Instantiate” is a fancy word that means “make.” The first argument in the parenthesis is the game object we want to make a copy of — our laser. The second argument is where we want to place this new laser; in this case, where our player currently is. That last part with the q-word is the rotation we want to apply to the new laser. Human minds don’t like working with quaternions, so we use “.identity” to keep it as is. When we want to rotate stuff we’ll be using Euler angles — they’re slightly easier to work with.

Huh?

The player sees the lasers go off the top of the screen. But we don’t want EVERY laser ever fired clogging-up precious CPU resources. Luckily, destroying an object is super-simple. If you look at the top image, you’ll see all the lasers abruptly disappear when they hit a certain point in the developer’s Scene view. Here’s how that was done:

You’re gone too far this time; you must be destroyed!

Human Translation: “If you’ve gone too high in the game world, destroy yourself.” Harsh, but effective!

Now you hold the powers of creation and destruction in your hands! Wield them wisely!

“You were doing well until everyone died.”

An independent Unity Developer.