A big part of game development is the constant management and adjustment of the scene and all of its contents. This process, more often than not, must be handled during gameplay when we get a clear view of how our project and code performs. In Unity, this means searching for objects in the Hierarchy and adjusting values to either test out new ideas or correct errors. You will find that your biggest enemy in this endeavor is clutter.
A top-down shooter like the one I am working on will very quickly see the Hierarchy become unmanageable with laser and enemy objects. However, with a few simple changes to our code, we can minimize this mess and optimize our workflow.
Organize our project Hierarchy by compartmentalizing our instantiated GameObjects.
The idea we will employ for this task is quite simple — take every object we create at runtime and organize it inside separate “containers” in our Hierarchy.
When we use the
Instantiate() method in our game, we are telling Unity what and where to create new objects in our scene. However, as these objects are created, they will simply be added to our Hierarchy list at the root level.
We can create a solution to declutter the list and organize our work.
First, we will create an Empty GameObject in our scene and give it an appropriate name. In my case, I am creating a container for my enemy GameObjects. I will name it “BasicEnemy”. For the sake of organization, I will make it a child object of my SpawnManager.
Now that we have our container in place, we can move on to coding a way to organize our GameObjects.
If you will recall, this is the Coroutine we are using to instantiate our enemies. We can make a few changes to it to reach our goal.
We begin by adding a
public Transform variable called “BasicEnemyContainer”. We can set the value of this variable in our
Start() method, alternatively, we can simply drag it into our Inspector’s variable field.
Next, we will have to get a reference for our newly instantiated GameObject. This is simple enough to do and takes very little effort.
We can get a reference by declaring a variable of Type GameObject and setting its value equal to the instantiated object. Once we do, we can now affect this new GameObject any way we want.
In this particular case, we will set its parent object to be the container we created earlier.
We are done! I will go ahead and do this for my laser GameObjects as well so that I may show you the results.
By simply creating a variable for the objects that we
Instantiate(), we can gain control over the new object and affect it in many different ways: We could use
GetComponent() to call any public method in one of its Components, change its name, assign it a different tag, etc...
For now, we have used the
SetParent() method to make any new object a child of our containers. The result is a cleaner folder-like structure in our Hierarchy that we can expand and collapse at will.
For more information on this method, I suggest reading the Unity API page for SetParent().