Spawn Objects without Clutter

So, your game is coming along nicely with your enemies spawning continuously…

Spawning enemies over time via coroutine

… but now your Inspector is getting a little busy.

Multiple enemy objects listed in the Inspector

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

Steps:

  • 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.

Create an Enemy Container object

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.

Create a variable to hold a reference to the Enemy Container object

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.

Cache a reference to the instantiated Enemy object

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.

Set the new Enemy object’s parent as 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.

Spawning enemies within Enemy Container

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.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Designing the Swiggy app to be truly ‘accessible’ | Episode-2

A first look at Material You

Using Mobx in Flutter: Hot or Cold Game

Simple Android Architecture : LiveData and event

Internal Storage Android

Neumorphism in Android by ArunAndroid

Lessons learned when migrating my app to Jetpack Compose

The launch of a rocket with a blue sky background.

Improving app startup with I/O prefetching

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Erin Brown

Erin Brown

More from Medium

git it on the page!

Restarting a Scene in Unity

How to Install Unity

A Guide to a More Productive Unity Layout