Parent Objects and Instantiating Children Objects

David Hunter Thornton
3 min readAug 23, 2022

--

How do I Instantiate a Game Object as a Child of another Game Object?

Objective: Explain Parent/Children Game Objects and how to instantiate an object with these properties upon spawning.

So at the moment I have a minor organization problem in my Hierarchy. When I spawn enemies, it eventually creates a very large mess. Add onto that spawning in bullets and things are very bad. So let’s discuss Parent/Children Objects.

You can think of Parent and Child Objects as a folder-like system. Where all of the children folders are located inside the parent or original folder. Additionally, if you move or do something to that parent folder, the children follow it. Let’s start with a simple example. I’ll create another Empty Game Object.

Here I’ve made an EnemyContainer Game Object, which once dragged ontop of the SpawnManager created in my last article, makes the EnemyContainer a child of the SpawnManager.

Now we just need to Instantiate all of our Enemy(Clone) Objects as Children of the EnemyContainer. And we can do the same steps for the Mine(Clone) and Bullet(Clone) Objects. Let’s start with a SerializeField to associate the container wither our C# Script.

And then drag and drop the Game Object into the Container Type Location.

I’ve already got the code needed to spawn my Enemy and Mine. It looks like this:

But at the moment its simply spawning it. And we have no way to access any information about that object that has been Instantiated. So we need to create a variable for that information to be stored into.

Now the information is being stored in “newEnemy”. And as we talked about in an earlier article, you can access any Game Object information through its “.transform” information and essentially use the Inspector as a “folder directory” of sorts.

We want to first get the information stored in “parent” and then “set parent” to the container.

Since our “parent” is of type “transform” then we need to include “.transform” after “_containerTypeEnemy” so that both types match.

Friendly Reminder: Don’t forget to keep updating your project through GitHub/Git and saving any changes you make to a separate branch. You can check out my other articles to get a breakdown of those steps! Day 1–9

--

--