Parenting Spawned Objects
My Goodness, have you noticed that with the spawning manager the more enemies you have the more cluttered the hierarchy gets? It’s a mess, and it simply won’t do. We need to tidy this up so we can stay better organized and maintain the program as it grows.
What we want to do is instantiate the enemy then have them ‘childed’ into the spawn manager. To do this we must first create an empty object inside the spawning manager to define the he container for the enemies. To keep things simple name it EnemyContainer.
To hold onto a reference of the instantiated object once it is instantiated, we need o store it in a GameObject type variable. In the front of out instantiate line simply add GameObject newEnemy =
Once the new object is created, we can access it through the newEnemy. Now we need to set the parent of the newEnemy.
With this the enemies will now spawn under the spawn manager keeping the hierarchy clear from clutter.
So, when we add more things to spawn, such as power ups, we will have plenty of room to work with and now we know how to keep them tidy when the time comes. Hunting down a bug is always easier if you have a clean and organized landscape to work in. As always, don’t be afraid to experiment with code and I will see you in the next one.