Instantiating & Destroying Gameobjects in Unity
This article will look at creating or instantiating a gameobject and then destroying it so the game does not become cluttered with copies. To show this, we will create a shooting system that will spawn a prefab of a laser. The Laser will then travel upward, and after it has gone off so far off the screen, it will be destroyed.
Let’s begin with creating the Laser. Create a Capsule that will act as the Laser, then name it “Laser” and make it 0.25 on all Scale values. At this point, you can create a material for the Laser so you can see it easily, but it is not necessary.
Now to turn the Laser into a Prefab. A Prefab is just a way to make a gameobject that you can use across the project with the default set values and components you give. Making a Prefab is easy. You drag the gameobject that you want to turn into a Prefab into the Assets folder, but it is best to stay organized and have a folder in the Assets folder just for Prefabs. After you have made the Laser a Prefab, you can delete it from the scene.
Now we can start making the code to spawn the Laser. I am going to put this code on the Player since that is what is firing the Lasers. First, you need to create a variable that will represent the Laser Prefab that we will spawn. Next, in void Update, make an if statement looking for the space key to be pressed. Then inside the if statement, we will use Instantiate to spawn or create a new laserPrefab at the position of the Player with transform.position and at the same rotation values as the Prefab with Quaternion.identity.
Now back in Unity, select the Player and drag the Laser Prefab into the Laser Prefab gameobject field and then you can enter Play Mode and fire away.
Now that the Laser is spawning, let’s get it moving. To do that, create a new script named Laser and attach it to the Laser Prefab.
Now in the Laser script, we will program it to move. First, make a public float named speed with a value of 8. Then, in the void Update, we want to adjust the Laser’s transform every frame, moving upward. We will use Translate and move it by Vector3.up, which is the same as new Vector3(0, 1, 0), then multiply it by the speed variable and multiply that by Time.deltaTime.
Now the Lasers will fly upward and off the screen, but they begin to clutter the Hierarchy quickly, as you can see.
With the Lasers moving, let’s clean them up after they go so far off the screen, so they don’t clutter up the scene. In the Laser script, inside of void Update, after the movement code, add an if statement that checks it the Laser’s y position is greater than 8, which will put it off the screen. Inside the if statement, have it Destroy the Laser gameobject once it exceeds 8 on the y.
And now the Lasers will destroy themselves after they go past 8 on the y axis.
And there is a simple example of how to instantiate and destroy gameobjects in Unity. In the following article, we will expand the firing system to have a cool-down.