Spawn Objects without Clutter
So, your game is coming along nicely with your enemies spawning continuously…
… but now your Inspector is getting a little busy.
Let’s take a look at how to organize enemy instantiation to reduce clutter in the Inspector.
Objective: Spawn enemy objects in a container to reduce impact on Inspector
- Create an Enemy Container
- Get a reference to the Enemy Container
- Get a reference to the spawned enemy
- Set the Enemy Container as the spawned enemy’s parent
Create an Enemy Container
To create an Enemy Container, you can create an empty object in your scene. Since this empty object will be a parent to other objects, be sure to zero out the position and rotation data to avoid any issues.
You can set the Enemy Container as a child of the object that holds your spawning script (ie. Spawn Manager) to keep things more organized.
Get a reference to the Enemy Container
In your Spawn Manager script, create a new class variable to hold a reference to the Enemy Container.
You can assign the Enemy Container in the Inspector if you serialize the variable.
Get a reference to the spawned enemy
To manipulate the spawned enemy after instantiation, you’ll need a reference to the enemy you created. To do this, cache the result of the Instantiate call in a variable within in your Spawn Routine.
Set the Enemy Container as the spawned enemy’s parent
With a reference to both the Enemy Container and the newly spawned enemy, you can set the parent of the spawned enemy to be the Enemy Container.
Much like manually moving the Enemy Container within the Spawn Manager object in the Inspector, this will store each new enemy under the Enemy Container, which can be collapsed in the Inspector.
Keeping your Inspector clean and organized will make it easier to manage your project as it grows. Remember: There’s a place for everything and everything in its place.