Spawning Objects in Unity without the Clutter
Many times our game development involves creating “GameObjects”, such as bullets, arrows, or missiles, during gameplay. This process is called instantiating. Every object we create gets displayed in the hierarchy. Just add ten or twenty bullets and shell casings, and things can get cluttered fast. To avoid this we can create an “Empty GameObject” and assign it to be the “parent” of the GameObjects instantiated. In our example, we will call our new “parent” “Enemy Container” As we spawn enemy ships they will appear under the “Enemy Container”, which will be located under the “Spawn Manager”

Let's create some variables. We have created three, “private GameObject _enemyPrefab”, “private GameObject _enemyContainer” and a “private bool _enableSpawn”, which is set to “true”.

We are using a Coroutine I named “SpawnRoutine”. This routing gets started once and keeps running throughout the game and creates a new enemy ship every five seconds.

Within our SpawnRoutine we create a local “GameObject” called “newEnemy” and assign it the identity of the instantiated “_enemyPrefab”. Once we have this identity we can use it to assign the newly created “_enemyPrefab” a new parent.

Since “newEnemy” is an alias of “_enemyPrefab” we can use it to assign the new parent. The line of code in blue above makes “newEnemy” a “daughter of the “parent” “_enemyContainer”. Remember a “transform” and a “gameObject” means the same thing, two names for the same identity.

The newly created “Enemys” will not be visible unless we click on the > just left of “SpawnManager”

This keeps things uncluttered and will allow us to use the “hierarchy” window a little more effeciently…