CodeX
Published in

CodeX

Unity3D: Eliminate Clutter when Instantiating many Objects

Sometimes we need to eliminate clutter and keep things simple when viewing the Hierarchy panel. This way, we can have our environment tidy and clean during Play mode.

Having the enemies spawn and recycled when not destroyed will create clutter.

The objective is to spawn the enemies within a container and act as the child object of the parent object.

Let’s create a new child object under our Spawn Manager game object.

Create a child object in Spawn Manager where all instances of a defined object will Instantiate to.

The Spawn Manager has a script attached to it that has the logic for instantiating the enemy.

We need to store a reference where this child game object (Enemy Container) is located. So we must initialize and declare an object of type GameObject.

private GameObject _enemyContainer = null;

Let’s add an [SerializeField] attribute so that we can see it in our Inspector panel.

[SerializeField] private GameObject _enemyContainer = null;

Let’s drag and drop the Enemy Container object into the Enemy Container field in the inspector that is visible when selecting the Spawn Manager game object.

We then need to cache where we Instantiate our enemy objects. By caching, I mean storing it into a variable so that we can use it elsewhere. In my case, my Instantiate logic would be in a coroutine named “SpawnRoutine.”

 private IEnumerator SpawnRoutine()
{
while (!_stopSpawning)
{
float randomX = Random.Range(-11, 11);
Vector3 randomXposition = new Vector3(randomX, 8, 0);
Instantiate(_enemyPrefab, randomXposition,Quaternion.identity); yield return new WaitForSeconds(_waitTime);
}
}

To cache the instance of the enemy that is being Instantiated, we need to assign a GameObject datatype named enemyInstance.

GameObject enemyInstance = Instantiate(_enemyPrefab, randomXposition, Quaternion.identity);

We now have our instantiated enemy stored in the enemyInstance variable. We will use it in the next line of code.

We want to instantiate our enemy instances as a child of our Enemy Container game object. We have a reference to the Enemy Container called _enemyContainer, and we attached the game object to the field in the inspector earlier.

So we need to grab the enemyInstance and its transform parent and assign it to the _enemyContainer’s transform.

enemyInstance.transform.parent = _enemyContainer.transform;

What we are saying in this line of code is:

Hey, enemyInstance, I want you to move into _enemyContainer’s transform location.

Remember, every game object has a transform component, and every transform component is a game object. By grabbing _enemyContainer’s transform, we are suggesting that we want the enemyInstance to relocate into the _enemyContainer game object.

When we are in Play mode, we can now see our Hierarchy window tidy and cleaner!

Tidy and Clean. The Hierarchy is happier and so are you as a programmer!

That is all for today! Thank you for your time. :)

--

--

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
Gabriel Perez

Gabriel Perez

102 Followers

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.