GameDev : clean up Spawn Coroutine

Matteo Lo Piccolo
4 min readMay 27, 2021

After setting up Spawn Coroutine, we need to clean up a bit before moving on.

First, if we press Play and look at hierarchy, we notice this problem

Every time Enemy Spawn, a clone of our prefab appear into hierarchy.
This is not good, because if we have many of Enemy, or more probably, Laser (or projectile), the hierachy becoming a complete mess…

As always there are more solution.
I prefer set up simple things for now.
One good solution is create an Empty Object under our Spawn Manager, end make Enemy becoming CHILD of that object.

This is another important concept in Unity : PARENT AND CHILD.

Is not difficult to understand, and it is the best way to manage our hierarchy and as always, keep things easy and clean.

To do this, under our Spawn Manager we can create an Empty Object

As we noted, the item is now DIRECTLY ATTACHED under Spawn Manager.
In Unity, this relationship is referred to as PARENT and CHILD.
Spawn Manager is Parent, and the Empty Game Object is a Child.
It is a simple terminology for describing the various relationships in the Hierarchy.

I called it “Enemy Container”.

Like everything else, we need a reference to this Enemy Container

And drag and drop it into the Inspector

Now that we have the right reference to it, how can we make the Enemy Spawn DIRECTLY UNDER THE ENEMY CONTAINER?

We look at this part of code

Within Coroutine we instantiate the enemy and check their position, rotation, etc., okay?
But to HAVE DIRECT ACCESS TO THAT OBJECT, we need ANOTHER REFERENCE.

In simple terms, we create a variable which CONTAIN THE REFERENCE to Enemy

We change the code above to this

Thanks to newEnemy variable, we can ACCESS DIRECTLY to Enemy object.

So we now can take newEnemy and make it CHILD of Enemy Container

To set up this directly in code, we need the reference to the Container and the reference to that Object, and we have both.

The next line of code is :

SetParent() method give us the possibility to set up PARENT AND CHILD RELATION DIRECTLY IN CODE.

It ask us a TRANSFORM parameter inside it.

If we pass our Enemy Container it give us an error. Why?

This happen because Enemy Container is a GAMEOBJECT, but SetParent ask us a TRANSFORM.

We can solve this in two ways : change the type of Enemy Container from GAMEOBJECT to TRANSFORM.
It can be a solution, just remember : if we change the TYPE and than SAVE, we have to DRAG AND DROP AGAIN in Inspector because after change it, we lost the REFERENCE TO IT.

If we drag and drop again, it works.
I don’t think it’s great, but it’s a way to do it.
For me, the easiest way is this
Reset TYPE on GameObject

Inside SetParent() pass this

And everything is absolutely ok!

There is an alternative syntax to do this

Exactly the same thing.

In this example PARENT is a PROPERTY with GET AND SET, so we can ASSIGN this transform to another.

SetParent(), instead, is a METHOD of TRANSFORM CLASS.

We don’t worry too much now, I still have to explain what properties are and how to use them, but for “basic knowledge” I try to explain why in the method we pass TRANSFORM in brackets while in the other we simply ASSIGN one thing to another.

If we now make a try

Every time an Enemy spawn, now is a CHILD of Enemy Container!

--

--

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!