Spawning Items in Unity for Neatness

Bob Hilbig
3 min readJul 26, 2021

For a simple 2d shooter, there should rarely be a case where your hierarchy is cluttered beyond usability but that doesn’t mean you shouldn’t work on good habits.

Hard mode!

Fine tuning a project, or even just finding elements in the game can be a chore if you don’t organize!

What are the tools we have for organizing our game objects?
1. Containers
2. Prefabs of Prefabs
3. Cleanup

Containers

If we start putting all of our enemies into the enemy container we can access them a lot more quickly, and we can find other objects in the hierarchy more easily too. An easy way to do this is using an empty game object. Then whenever we create an enemy, we can tell it that this empty game object is it’s new parent.

Are you my parent? Yes I am.
Neat and Tidy.

Prefabs for Prefabs

This works in a similar way. But when you need to create a group of objects on a consistent basis, just create a new prefab that contains all the other prefabs.

Shoot one get two free!

Our triple shot behaves in this way. Don’t instantiate the prefab three times, instantiate the Prefab of the Prefab once.

Cleanup

We already have covered deleting objects when they aren’t needed but the containers can also be deleted as well. Here is a great way to detect when a parent container should be deleted, when there are no children!

Unity keeps track of how many children exist so let’s use this to our advantage. While some objects call for deletion when they are off camera, these invisible objects need to be treated differently for cleanup.

Don’t make a scene more hectic than it needs to be. Find a way to use containers and parents as a way to organize your game objects. It gives you a pleasant debugging experience as well as great control over your objects with native functions.

--

--