#4 Create and Destroy GameObjects in Unity at Runtime

Introduction

In this arti­cle we're going to look at how to cre­ate and destroy GameOb­jects at run­time. Achiev­ing this is very impor­tant for cre­at­ing games, because it allows us to place objects when and where we want and then destroy them.

Go to the project’s main page

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

Procedure

In this chal­lenge we are going to use the Creation/Destruction sta­tion shown in fig­ure 1, to make a small Lucy appear and dis­ap­pear cycli­cal­ly while the game is running. 

unity 3d project, 3d model made in blender of a capsule with a small person inside. on the wall a sign that says: creation/destruction station". methods instantiate and destroy in unity
Fig. 1: Cre­ation Destruc­tion Sta­tion of the GameDevLab.

unity 3d project, 3d model made in blender of a capsule with a small person inside. instantiate and destroy methods in unity
Fig. 2: Cre­ation Destruc­tion Sta­tion of the GameDe­vLab. Lucy appears for a few seconds.
project unity 3d, 3d model made in blender of a capsule, the person of the interior has disappeared. methods instantiate and destroy in unity
Fig. 3: Cre­ation Destruc­tion Sta­tion of the GameDe­vLab. Lucy disappears. 

Fig­ures 2 and 3 illus­trate the cycle of Lucy's appear­ance and disappearance.

Fig. 4: Fields of the Cre­ation­De­struc­tion Script cor­re­spond­ing to the chal­lenge of video 4 of the Fun­da­men­tal Series.

Fig­ures 1 and 2 show the fields and com­po­nents in the "Cre­ation­De­struc­tion" Script inspector.

Fig. 5: The GameOb­ject "#4 Cre­ation Destruc­tion" is assigned the Script Cre­ation Destruc­tion. This is seen in the inspector.

The chal­lenge is to com­plete the cre­ateOb­ject() and destroy­Ob­ject() meth­ods of the Script.

Resolution

Creating the object

For the cre­ation of the object we are going to use the Instan­ti­ate method, this method is defined in the MonoBe­hav­iour class that by default is the super class or par­ent class of every new Script that we make in Unity. 

At the moment I don't have enough arti­cles or videos about object-ori­ent­ed pro­gram­ming to clar­i­fy the super class­es, I know I will even­tu­al­ly do them, but for now if you want to know more research the top­ic Her­itage in object-ori­ent­ed programming.

The Instan­ti­ate method has more than ten vari­ants, but in all of them we basi­cal­ly need two things: the object we are going to cre­ate and where in the space we are going to put it.

Click here to read an arti­cle on pro­gram­ming methods.

In this case the object that we are going to cre­ate we indi­cate it using the pre­fab Smal­l­Lucy that is in the fold­er Inter­nal Use, this pre­fab we place it in the space Object To Cre­ate in the inspec­tor, as it is observed in the fig­ure 5.

To indi­cate the point of the space where we are going to place the new object we are going to use a GameOb­ject called Posi­tion that is in the hier­ar­chy. We also assign it in the inspec­tor (fig­ure 5).

We are going to use a vari­ant of the Instan­ti­ate method that will cre­ate the object and place it in the hier­ar­chy as a child of the GameOb­ject we indi­cate. This means that our object will appear in the posi­tion of the par­ent GameOb­ject (Posi­tion) plus the own dis­place­ment that Smal­l­Lucy has (in this case Smal­l­Lucy is in the ori­gin so its dis­place­ment is zero).

I know that the para­graph above is some­what con­fus­ing, but lat­er I would like to make a video and arti­cle about glob­al and local coor­di­nates in order to explain this more clear­ly. For now, let's move on. We write the fol­low­ing line in the cre­ateOb­ject method:

cre­ate­dOb­ject = Instan­ti­ate (objectToCreate,position.transform);

First of all cre­ate­dOb­ject is an aux­il­iary GameOb­ject type object (see fig­ure 4) that we define to save the ref­er­ence of the new object we are going to cre­ate. This is very impor­tant because oth­er­wise lat­er we won't know which object we have to destroy.

Find the ref­er­ences of the GameOb­jects from the Hierarchy

The Instan­ti­ate method will return a GameOb­ject type object (the new object) and with the equal sign we assign it to createdObject.

In paren­the­ses indi­cate the two para­me­ters sep­a­rat­ed by a com­ma. The first para­me­ter is the object to cre­ate and the sec­ond is the Trans­form com­po­nent of the GameOb­ject Position

Fig. 6: Meth­ods defined in the Cre­ation­De­struc­tion Script cor­re­spond­ing to the chal­lenge of video 4 of the Fun­da­men­tal Series. 

Destruction of the object

To destroy the object we use the Destroy method (which is defined in the MonoBe­hav­iour class) and give it as a para­me­ter the ref­er­ence of the object we have saved in the cre­ate­dOb­ject field. We write this in the destroy­Ob­ject method.

Cyclic behaviour

We want Lucy to appear and dis­ap­pear con­tin­u­ous­ly in the Cre­ation Destruc­tion sta­tion, so we have to run the cre­ateOb­ject method. Then wait a few sec­onds and call the destroy­Ob­ject method, then wait and call the cre­ate method again.

We did this first by call­ing the cre­ateOb­ject method from Start, so that when you start the game, Lucy appears. Then at the end of the cre­ate method we use Invoke to call the destroy­Ob­ject method after two sec­onds. We do the same thing at the end of the destroy­Ob­ject method, we use Invoke to call the cre­ate method. We can see this in fig­ure 6.

Conclusion

We have man­aged to make new objects appear on the stage at run­time, that is, while the game is run­ning, this is no small thing, under­stand­ing this we can achieve many things.

To cre­ate a new object in the world we basi­cal­ly need a copy of the object to cre­ate, this we achieve with a pre­fab. And we have to know exact­ly where to place it, this can be achieved in many ways, that's why the Instan­ti­ate method has so many variants.

If we want to destroy a GameOb­ject of the hier­ar­chy we need to know what it is, so it is very impor­tant the object ref­er­ence, if we cre­ate the object with­out assign­ing it to any field, the object will be in the hier­ar­chy but we can not do any­thing with it unless we find it again.

Scroll to Top
Secured By miniOrange