A Tidy House is a Happy House….

Yesterday, we looked at using coroutines to continuously spawn enemies over time. Having a continuous stream of things to shoot is fantastic for our player but the result in our Unity editor can get quite messy over time! Today we are going to clean up our hierarchy and place all those spawned objects into a container object!

First we’ll create our container object by right clicking in the hierarchy window and selecting “Empty Object”. Then we’ll name our object “Enemy Container”.

Now that we have a place for enemies to spawn we need to specify for them to spawn there.

Unity’s Instantiate method has a 4th, optional parameter that allows you to specify the parent object of the object you are instantiating. We can use this to specify the container for our enemy objects to spawn in. First we need to create a variable for our enemy container object so that we can assign it in our instantiating code.

Now that we have access to the container, we can include it in the Instantiate call like so

Note that the signature for this overload of Instantiate calls for a Transform object, so I passed in the containers Transform. I could have just as easily made our serialized field of type Transform. I personally prefer to use GameObject where I can instead of Transform. It makes more sense in my head when thinking through my code.

Now as our enemies spawn in they will spawn in under our container!

Next Time!

There we are! We have cleaned up our hierarchy and our enemy objects now spawn under a parent container. Tomorrow, we take a look at moving from our primitive prototype to a version with graphics! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.

--

--

--

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

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

Recommended from Medium

Convert word to pdf using AWS Lambda

Troubleshooting A Replica-Set in Virtual Environment

Flutter Databases: a look at the options from sqflite to ObjectBox

a review of databases for flutter

The 10x DevOps Engineer

KASP Rule — A rule to review a PR

Using rubocop-ast to transform Ruby files using Abstract Syntax Trees

Test-Driven Development: all about the cycle

Updates to my Flatiron Final Project

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
Christopher West

Christopher West

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

More from Medium

OnCollisionEnter() vs. OnTriggerEnter()

Platformer: Simple Movement & Jump

Creating a UI Shield Strength indicator

My journey becoming a Unity game developer: 2.5D Infinite Runner-Ledge Grab System-Pt3