Instantiating & Destroying Gameobjects in Unity

Hey! This is my 5th day on this journey. I actually wanted to observe my pace myself in order to design my end time more properly. Now you have it! I am aiming to build up the necessary skills in order to be an industry ready game developer in 120 days. So far so good, I will keep up the pace!

I have always wondered how do we actually fire bullets in games. During my youth, I played Counter-Strike:Source for many years and I really felt like I was really firing a gun with a mag holding bullets (well, not a killer when you look at it now :) ).

I really have installed and fired some shots just before publishing this

Bullets are just an example of game object creation during runtime. So how do we create objects like bullets during gameplay?

The idea is simple, we simply create a new game object or use an existing in a pool and give it a speed.

To create a new object during runtime, we use “Instantiate” in Unity. With Instantiate, we are able to create a predetermined game objects as the game continues. So how are we going to predetermine these objects?

That’s where Prefabs come into play in Unity. Prefabs are basically predetermined game objects that are ready to be used.

To be organized, we should create a “Prefabs” folder in our Projects tab. Creating a prefab of a game object is just by dragging and dropping a game object to projects window.

We will notice that the game object we have created a prefab will now have a blue color in our Hieararchy.

Prefabs are just templates that makes our lives easier. A prefab stores all of the information of an object up to the time it has been created as a prefab. If you are designing a game that has different game objects that share some amount of information, a prefab will be handy for you.

In order to use the created prefabs in a proper way, we need to know their logic behind it. For detailed information, you can check Unity’s documentation by clicking here.

If we change the properties of a game object via its prefab, it will be applied all of the game objects that exists on the scene. However, if we make a change on an existing game object in the scene, the prefab will not be updated automatically. Thus, making changes on a prefab is not the same as making changes on a clone existing in the scene view that is created from a prefab. This makes sense as we need to perceive the prefabs as templates. Luckily, Unity makes it possible to update the prefab that has changed from the scene view by using “override”.

If you ever want to apply the changes you have made to an instance of a prefab in the scene, you can simply update the prefab by clicking “Overrides — Apply All” from the inspector.

Unity is clever for sure, but it needs our guidance to know the object that we want to instantiate during the runtime. Since we are building a Space Shooter game and our ship will fire lasers, we will instantiate lasers. First, we make a prefab of our lasers and place it under our Prefabs folder in the Projects panel to keep everything proper.

[SerializeField] private GameObject _laser;

Since we want the laser to be special to the Player script, we have defined a private GameObject variable with SerializeField attribute in order to manipulate it from the Inspector.

Please note that we should always drag and drop the prefab from the project tab, rather than the scene view, as I have mentioned earlier.

As our script now knows about the laser we are going to initiate, we can continue to coding. First of all, we need to decide when we want the laser to be initiated in the game view? For this game, we want our cube (Hey! It will be a spaceship OK?) to fire a capsule (Alright! LASER!) when we hit the Space Key.

Unity has every possible scenario for key movements: pressing down, holding and leaving the button up. “Input.GetKeyDown” will be the function that we are going to use in this case.

if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(_laser, transform.position, Quaternion.identity);
}

With this code, we instantiate a laser gameobject, at the position of our player. Quaternion.identity is the part that defines the rotation of the instantiated object. Quaternion.identity simply means we want no rotation on the created object.

If you create a laser script and make the lasers move upwards once they are initiated, you will observe that they are being initiated at the center of our Player gameobject. In order to fix this, we need to find the optimal location to create the lasers by trial and error. In my case, the optimal solution has been as below:

Instantiate(_laser, new Vector3(transform.position.x, transform.position.y + 0.6f,0), Quaternion.identity);

Success! You are now firing the lasers!

Destroying The GameObjects

It is actually very simple to destroy the game objects in Unity. Simply by typing Destroy(gameObject). If you want to destroy the gameobject with a delay, we can state the seconds to count before destroying the game object by :

Destroy(gameObject, 2); 

For instance, to destroy our game object if it takes damage with 1 life left, we will type :

Destroy(this.gameObject);

For more information regarding Destroy, you can check Unity’s documentation here. A use case of Destroy will be included in my collisions and triggers article.

Wish you a great day!

Unity Developer