Spawning Objects in Unity without the Clutter

Renato Figueiredo
2 min readDec 1, 2023

Objective: Learn how to spawn objects without cluttering our hierarchy.

When we have for example a Coroutine, constantly spawning new enemies in our game, you can see that our Hierarchy starts to get clutter, as more objects are spawned.

We can see as more enemies are created, our Hierarchy is flooded with new objects.

In order to prevent this, we can create an empty GameObject, which is going to hold all new enemies being created, and assign it as their parent object.

There are two ways we can do this:

GameObject newEnemy = Instantiate(prefab, position, Quaternion.identity);
newEnemy.transform.parent = container.transform;

In the example above, when we instantiate a new enemy (prefab), we are assigning it to a new variable of type GameObject. Afterwards, all we are doing is assigning a new parent to our new enemy.

Instantiate(prefab, position, Quaternion.identity, container);

Now the simpler version of this is simply assigning the parent on one of the overloads we have for the Instantiate method. This way, the object being instantiated has its parent assigned for our container directly.

By using a container, we prevent the clutter and can easily see our Hierarchy.

Now that we’ve done this, we can Collapse our container in our Hierarchy, allowing to see all objects that it parents, or just closing said view and making clearer to our Hierarchy as a whole.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response