Spawning Objects in Unity

Simon Leen
4 min readMay 30, 2021

--

Enemys spawning fast

Objective: Create an enemy spawn manager.

When spawning multiples objects with a logical pattern, using something like a spawn manager can be useful. Start with creating an empty game object in the hierachy, rename it and then create a spawn manager script and attach it to the spawn manager object. The position of the spawn manager isn’t relavent as it wont be visable or interactable within the game.

This spawn manager will be for the enemies. Within the script set a private game object for the enemy prefab and name as such. Make sure to serialize it so that the prefab can be assigned in the inspector.

Enemy game object in spawn manager

Next to set up a spawn routine using an IEnumerator(Coroutine), explained in a previous linked post. For game logic, the enemies should start spawning on game start or from a triggered event but for now we’ll just go from the game start. The enemies should spawn one every 5 seconds from just outside the top of the screen moving into the scene and moving down and out of scene at the bottom. All enemies leaving the scene from the bottom will be moved back up to the same x-axis position but just outside the top of the screen to keep the enemies in play until destroyed. The enemies should keep spawning until the player is destroyed.

To have the spawn manager only spawn enemies while the player is alive, we set a bool variable called stop spawning and set it to false as we want it to run from the start. This will be set to true when the player gets destroyed. To change this create a public method called on played death which changes the bool to true. We set it public as this will be called by another script.

Bool and Method for spawning control

For the IEnumerator, set a while loop to control when the spawning will stop and use the stop spawning bool as the control. Within this create an enemy spawning position to use when instantiating the enemies. For the enemies, we set a random variable along the x-axis and set the y-axis position just above the scene. Set a yield return to 5 seconds to allow time between each enemy spawning. This could be set as a variable to allow for difficulty settings but for now, a set wait time will suffice.

To keep the hierachy clean while in play mode, so that 20 or so enemies don’t fill up the window, we can create an enemy container to spawn them within. Right click on the spawn manager and create an empty object, rename it enemy container and assign it using a serialized gameobject just like the enemy prefab within the spawn manager script.

To place each spawned enemy inside the enemy container, when instantiating the enemies, set them as game objects then set each ones transform.parent to the enemycontainer. transform. Then set the coroutine to start when the game does in the start method.

Start the coroutine

Now the IEnumerator has its conditional while loop, a spawning position, enemies being instantiated and having their parent object set to the enemycontainer and a waitforseconds applied, we move on to the player script to call to the spawnmanger when the player dies and stop the coroutine.

IEnumerator for spawning enemies

Inside the player script we have a method called take damage. The player has 3 lives and each time an enemy hits the lives reduce. We need to run a check so that when the lives are gone it player script calls the on player death method in the spawn manager.

Checking live count then calling the on player death method

For this to work we need to create an associated variable linked to the spawn manager so, we create a private spawn manager in the player script and assign it in the player script start method using the GameObject.Find to the get the spawn manager component. As a precaution and as a best practice, we run a null check on the spawn manager just to clarify it has found it and if not warn the developer in a log. Once this spawn manager component has been assigned to the spawn manager object, we can call it within the players take damage method and call the on player death directly.

Assigning the spawn manager

In the next post, we will swap out the cubes and capsules for game assets and add some visual elements to the project.

--

--

No responses yet