Scene Cleaning — Spawning Objects in Unity Without the Clutter

Heathrileyo
5 min readNov 24, 2021

--

Welcome back! It’s almost Thanksgiving and my family will be taking care of the mashed potatoes and green beans this year. I’ve bought the ingredients and we just need to cook them Thursday morning before we go to my mother in laws house. If there is one thing my kitchen needs, its some tidying up. There are toys in the floor from the boys, dishes, utensils, and food laying about. If I want to see the bigger picture before I start cooking, I’ll need to do some cleaning. In Unity when you start instantiating objects you’ll see that each clone appears in your hierarchy. This is great so that you can see what is appearing, but also difficult as it can fill and clutter you hierarchy fast. Today we’ll be looking at a way to manage your clones in a nice one stop shop, and to stop spawning clones when they player dies.

When you add different enemy types, collectible points/power ups, this could add up fast

First step is to make a new empty object called Enemy Container and make it a child of the spawn manager. We’re going to make all our enemy clones children of this new enemy container. That way you can minimizing and maximize that list as needed, easily cleaning your hierarchy. Our first step will be to add a new public game object enemyContainer to the spawn manager. In Unity we’ll click and draw our Enemy Container object to the new public game object spot we just added on the spawn manager object. Next, in the while loop, we’ll want to set a game object, newEnemy, equal to the instantiated enemies. This will let us both instantiate the enemies and reference them after they’ve been made. Once we’re able to reference them, we’ll set them equal to the enemyContainer.transform. Let’s see what that will look like.

We have our new public GameObject at the top, in the while loop we’re setting a gameobject newEnemy = the instantiating code, and we’re using that new reference to set the enemies as children of the enemyContainer. It’s a lot but we took it a step at a time and got where we needed to be!

Our next step will be to stop more enemies from spawning when the player dies. There will actually be a couple steps to this one. First we’re going to make a private bool at the start of spawn manager we’ll call it _stopSpawning and set it = to false. Now we’ll go to our while loop and instead of making it always equal to true, we’ll make it while _stopSpawning is false. This way it starts as true and spawns. Now we’ll need to make a method to change _stopSpawning to true when the player dies and end the spawning. We’ll make a public void method OnPlayerDeath(), and in the method we’ll make _stopSpawning true.

The next step is to find a spot where the player dies and call this method. Do you remember where we checked the lives of the player on the player script and destroyed the player if it was less than 1? So back to the player script and let’s make a private SpawnManager _spawnManager;. We’ll need to set this new _spawnManager in the player scrip to the SpawnManager gameobject in the hierarchy using GameObject.Find(“SpawnManager”). Your spawn manager in the quotations must be spelled exactly how you have it spelled in the hierarchy. Now we’ll use .GetComponenet<SpawnManager>(); to grab the SpawnManager script component. We can also use a null check here to see if the script failed to grab the component. We’ll use an if statement and if the _spawnManager == null, then produce a debug.logerror message to show in our console. The last step we’ll take is down where the game checks for the players life count on the Damage() method. We’ll add a line to if the player’s lives are < 1, and add _spawnManager.OnPlayerDeath(); and this will call the method we made back in the SpawnManager script to stop spawning enemies. Let’s look at all the changes we made to the player script.

Let’s break it down one more time like we usually do. You’ll see at the top we made a way to refer to our SpawnManager with the private reference called _spawnManager. Next in the void Start we set _spawnManager to the component scrip SpawnManager by first using GameObject.Find to find the GameObject then GetComponent to get the specific compenent. We also made a null check here to make sure that our code was finding something, and if it doesn’t, a message would appear in our console log. Lastly we went to the Damage method and added a line to call the OnPlayerDeath method from the SpawnManager script using _spawnManager from the player script. This way when the player dies we can call OnPlayerDeath from the player script.

Wow you’ve made a lot of progress today! I hope you’re ready for some family time coming up soon. We’ll keep learning step by step. See you again soon!

--

--

Heathrileyo

Full time father of two attempts to fill your prescription and become proficient in the world of game dev