Over the course of your Unity career you’ll be creating destroying and creating (known as Instantiating) tons of objects. This tutorial will show you how to do this with a simple example of spawning spheres both on command and automatically as well as destroying them. So let’s get into it.

First things first, start a fresh project or a fresh scene in a new project and let’s start by making a simple Sphere which we can do by right-clicking the Hierarchy > Create > 3D Object > Sphere. Reset it’s transform to get it to 0, 0 ,0.

Next let’s add a Rigidbody so it falls when we hit play. Make sure that “Use Gravity” is checked on the rigidbody.

Next let’s make a new script named “Create” or whatever you’d like. In this script we’ll add a private GameObject with a [SerializeField] attribute so we can see it in the inspector.

Now we’ll create a new empty object in the hierarchy name “Sphere Generator” this will hold our Create script from before. Add the Create script like so.

Now make a prefab of the Sphere we created earlier in the Project and then delete it so it’s out of the way. Take the prefab and slot it into the Sphere slot of the Create script on Sphere Generator.

Next return to the Create script and we’ll code the instantiation bit in Start.

This is the instantiation code. Let’s break it down. First you need to create a new GameObject that we called “newSphere” here. This acts as the container to hold the new object created.

Next we assign “newSphere” to Instantiate which has a few arguments before it will create the new object. You can learn all of the arguments here. In our example it needs a gameObject it’s copying, a Vector3, and a Quaternion (don’t worry too much about this, Quaternion.identity just means no rotation you can read more on Quaternions here)

Now we can see it in action and dropping (thanks to the Rigidbody giving it gravity) but that’s a little dull. Let’s make it drop on command.

Let’s take the Instantiate code and move it down to the Update method which ticks every frame but that would just spawn a new Sphere endlessly, let’s avoid that by wrapping it in a keypress. You can do that by using an if statement.

So breaking down the code above we have an If statement that takes Unity’s Input interface (here) and looks for a GetKeyDown (here) using the KeyCode.Space (here) to return the Instantiate code from above when the Space key is depressed.

Everytime you hit space it spawns another sphere. Now that we’re creating, let get to the fun part: destroying!

The simple way of illustrating this is by putting a script on our Spheres that tell them when to destroy themselves. Let’s start by creating a new script called “Destroy”

Now attach it our Sphere prefab in the project. Just click “Open Prefab” and it should show you an isolated example of the prefab in the scene. Just drag the Destroy or add the Destroy script to the sphere.

Now head over to the Destroy script in your IDE of choice and let’s get to wrecking this sphere based on a timer.

We’ll start by adding two floats, both private but one serialized so you can edit it called “currentTime” and “timer” respectively.

These will control the time it takes to destroy the sphere and then how much time has actually passed.

Next we’ll go to the Update method in Destroy and add time to current time. By default it starts at zero but as each frame passes more time is added to it by using Time.deltatime.

Now we have to add the critical part that tells Unity to destroy the object using a If statement to check if the currentTime is higher than the “timer” and if it is then to pass the Destroy() command.

Destroy() is a internal command to Unity that tell it to essentially Destroy the object. There are a few caveats to using it that you can check in the Unity scripting reference (here) and generally it’s better coding practice to reuse rather destroy an object.

Here’s the completed code, currentTime will keep track of how much time has passed and when it passes the set amount in timer it will destroy the gameObject (the sphere in this case) this script is attached to.

Be sure to set a time in the timer variable on the Sphere prefab otherwise it too will default to zero which will destroy it instantly.

Now let’s watch it go create and DESTROY!

There you have it. A fully functional instantiation and destruction system. With this you can make like a space shooter where your shots disappear after a short period of time, destroy on impact, or when they leave the screen. Here’s it working in Ataque as they hit a projectile removal object.

This should give you the tools you need to start creating (and destroying) Game Objects in Unity, experiment with setting time delays, using colliders to detect when objects hit one another to create or destroy objects. Until next time, happy coding.