Unity Guide

Spawning objects in Unity without clutter in the scene

A quick guide to handle clutter when spawning objects in a Unity scene

Objective: Avoid clutter inside the hierarchy of the scene in Unity.

Continuing with the previous post about how to spawn enemies infinitely using coroutines, we need to get rid of clutter inside our scene in Unity so that it doesn’t make the hierarchy look like a mess.

For example, if we run the last version of the last post we will see that when enemies are spawned in the scene the hierarchy looks like this:

Every clone of the enemy prefab will be stacked and it will make difficult to search for other gameobjects in the scene.

So, in order to avoid the above, we can hold all the enemy clones using an empty gameobject inside the spawn manager:

Once created, we need to create a new variable inside the spawn manager script (where the clones are created) to have the reference of the empty gameobject in the scene:

Remember that we can use [SerializeField] to show a private variable in the Unity’s inspector

Now save the script, go to Unity, select the spawn manager and then drag the empty gameobject into the variable of the script attached to the spawn manager:

Drag the empty object
This is the variable inside the spawn manager script
Once you drag the empty gameobject into the script component it will save the reference to use it in the code

Finally, open the spawn manager script and edit the next lines inside our coroutine:

The empty gameobject is set as the parent of each one of the clones spawned

As you can see, we need a Gameobject variable to save every clone spawned in the scene, so that every time this happens the empty gameobject gets set as the parent of the clone.

Now the clones look like this in the hierarchy and the clutter is gone:

Use this method to avoid clutter in every other gameobject spawned in the scene

And that's it! the clutter won’t be an issue as long as we keep the hierarchy items organized. I’ll see you in the next post, where I’ll be showing how to take your game in Unity into the next phase with new assets.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Fernando Alcantara Santana

Written by

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store