Spawning Objects in Unity without the Clutter

Day 10 — Aspiring Game Creator

2D Game Dev: Galaxy Shooter

Overcrowding !!!
Instantiation without organization leads to clutter!

Guidelines, standards, best practices.

They’re all here to help keep a project organized.

Whether it’s the adhering to C# standards when coding or creating numerous folders to organize assets, scripts, materials, prefabs, animations and so on, it’s best to start at the beginning of the project before chaos reigns.

Objective: Compartmentalize & Organize instantiated prefabs in the Hierarchy.

The easiest and fastest way to organize instantiated game objects is by re-parenting them to an empty game object.

Pseudo Code:

  • Create an empty container within the Hierarchy and reset its transform.
  • After instantiating the enemy prefab, store it into a temp variable.
  • Re-parent the temp variable to the container’s transform.

Below, the SpawnEnemy method instantiates the prefab and assigns it to the newEnemy variable. Next the newEnemy’s transform is re-parented to the enemyContainer.

Now, when the Spawn Manager runs, instantiated enemies are spawned within the Spawned Enemies container.

Crisp and clean!

BEST PRACTICE: Make sure to RESET the empty container’s transform, otherwise objects will suddenly relocate because of the inheriting properties of the new parent game object.

Aspiring Game Creator