Introduction
In this article we’re going to look at how to create and destroy GameObjects at runtime. Achieving this is very important for creating games, because it allows us to place objects when and where we want and then destroy them.
Go to the project’s main page
Procedure
In this challenge we are going to use the Creation/Destruction station shown in figure 1, to make a small Lucy appear and disappear cyclically while the game is running.
Figures 2 and 3 illustrate the cycle of Lucy’s appearance and disappearance.
Figures 1 and 2 show the fields and components in the “CreationDestruction” Script inspector.
The challenge is to complete the createObject() and destroyObject() methods of the Script.
Solving this exercise
Creating the object
For the creation of the object we are going to use the Instantiate method, this method is defined in the MonoBehaviour class that by default is the super class or parent class of every new Script that we make in Unity.
At the moment I don’t have enough articles or videos about object-oriented programming to clarify the super classes, I know I will eventually do them, but for now if you want to know more research the topic Heritage in object-oriented programming.
The Instantiate method has more than ten variants, but in all of them we basically need two things: the object we are going to create and where in the space we are going to put it.
Click here to read an article on programming methods.
In this case the object that we are going to create we indicate it using the prefab SmallLucy that is in the folder Internal Use, this prefab we place it in the space Object To Create in the inspector, as it is observed in the figure 5.
To indicate the point of the space where we are going to place the new object we are going to use a GameObject called Position that is in the hierarchy. We also assign it in the inspector (figure 5).
We are going to use a variant of the Instantiate method that will create the object and place it in the hierarchy as a child of the GameObject we indicate. This means that our object will appear in the position of the parent GameObject (Position) plus the own displacement that SmallLucy has (in this case SmallLucy is in the origin so its displacement is zero).
I know that the paragraph above is somewhat confusing, but later I would like to make a video and article about global and local coordinates in order to explain this more clearly. For now, let’s move on. We write the following line in the createObject method:
createdObject = Instantiate (objectToCreate,position.transform);
First of all createdObject is an auxiliary GameObject type object (see figure 4) that we define to save the reference of the new object we are going to create. This is very important because otherwise later we won’t know which object we have to destroy.
Find the references of the GameObjects from the Hierarchy
The Instantiate method will return a GameObject type object (the new object) and with the equal sign we assign it to createdObject.
In parentheses indicate the two parameters separated by a comma. The first parameter is the object to create and the second is the Transform component of the GameObject Position
Destruction of the object
To destroy the object we use the Destroy method (which is defined in the MonoBehaviour class) and give it as a parameter the reference of the object we have saved in the createdObject field. We write this in the destroyObject method.
Cyclic behaviour
We want Lucy to appear and disappear continuously in the Creation Destruction station, so we have to run the createObject method. Then wait a few seconds and call the destroyObject method, then wait and call the create method again.
We did this first by calling the createObject method from Start, so that when you start the game, Lucy appears. Then at the end of the create method we use Invoke to call the destroyObject method after two seconds. We do the same thing at the end of the destroyObject method, we use Invoke to call the create method. We can see this in figure 6.
Conclusion
We have managed to make new objects appear on the stage at runtime, that is, while the game is running, this is no small thing, understanding this we can achieve many things.
To create a new object in the world we basically need a copy of the object to create, this we achieve with a prefab. And we have to know exactly where to place it, this can be achieved in many ways, that’s why the Instantiate method has so many variants.
If we want to destroy a GameObject of the hierarchy we need to know what it is, so it is very important the object reference, if we create the object without assigning it to any field, the object will be in the hierarchy but we can not do anything with it unless we find it again.