Unity Coding — Create and Destroy

Christopher Graf
4 min readMay 6, 2021

In most video game worlds, there are many more elements that appear on the screen than the static ones that we there as soon as a level started. These can be enemies, projectiles, obstacles, collectibles, or almost anything else. Sometimes they disappear as quickly as they appeared, like Mario’s fireballs or Mega Man’s pellets. Today let’s look at creating and destroying objects in Unity.

First, we are going to tackle creation. In Unity creating an object is called ‘Instantiating.’ Go to the Project window and create a folder called ‘Prefabs’ in Assets. This will be maybe the most important folder contained in each of your Unity projects. Any object that you wish to instantiate later in code will need to exist as a prefab in this folder.

Simple pellet

For our purposes we are just going to create a 3D capsule in the Hierarchy. Feel free to change the size, material, color, or name. Once that is done, click and drag that object from the Hierachy into the Prefabs folder we just made. It turned blue! That means it is now a prefab. It is now a permanent mold for Unity to know whenever you want to make a new one. You wouldn’t want to create an entirely new fireball for each one Mario threw from his hands.

Go ahead and delete that object from the Hierarchy, but leave the prefab. Now comes the fun part. In script that you can either add to another game object in the scene or the Main Camera, whatever works for you, we are going to add a simple script. At the top of the script where you put in variables, declare either a public GameObject or a private GameObject with the [SerializeField] attribute. Now that script in the Inspector will allow you to click and drag a GameObject for that variable.

GameObject variable declaration

Go ahead and drag the prefab you made earlier into that spot. Now that scripts knows what that variable is, so we can make it whenever we want. Now we need to make that happen. Let’s add an input check into Update().

Input example in Update()

In this example, we want the prefab to be instantiated when the player pressed the Spacebar. Thankfully there is an easy to remember name to the method, which is just Instantiate(). So put in Instantiate() and inside the parenthesis whatever you named the GameObject variable.

Simple Instantiate

That’s it! If you run Unity and keep pressing Spacebar, new clones will continuously appear in the Scene and the Hierarchy. You can get more in depth with Instantiate() by adding a couple more arguments.

With transform.position in the second place, you are basically saying that this object should be instantiated at its default position. Usually for the prefab, this should be (0,0,0). That way it will simply appear right object that instantiates it, like Mario’s fireball. You can also adjust this with an offset by saying transform.position + new Vector3(1, 1, 0). Adding any numbers you want to the new Vector3 will allow any offset on the axes. Quaternion refers to the rotation. 99% of the time, you will be using Quaternion.identity, which simply keeps the default rotation.

Right now if you keep hitting Spacebar, the objects will all appear in the same space and stack up in the Hierachy. Too many objects at once is not always great, so let’s learn to destroy them now. Create a new script and place it on your prefab. Here is the real beauty of prefabs. Whatever script or other component you add to the prefab, will exist for ever instance, so we’ll only have to do this once.

In another act of helpful naming, the method for destroying an object is called Destroy(). If you place Destroy(this.gameObject) into any function, Start() or others, it will destroy the object the script is placed on.

If we do that, however, then the object will be destroyed as soon as we instantiate it and we won’t be able to have any real control. Of course you could place this under some condition as will most likely be the case in future works, but right now I want to look at another option. Just like the Instantiate() method, Destroy() also has an option for additional arguments. If you use Destroy(this.gameObject, 3.0f) it will destroy the object after approximately 3 seconds have passed. Place that into the Start() function and give it a whirl!

Look at that! Once you create some objects, once 3 seconds have passed, they simply destroy themselves. With this new knowledge, you are now a game developer with the power to create and destroy. Feels good, doesn’t it?

--

--