Clean up your Spawned Objects in Unity
It’s that time of year again (at least it is at the time of writing)…time for spring cleaning. It’s the time I see my neighbours cleaning their respective garages out, readying their pools and for some reason, a lot of mattresses sprawled out on the curbs.
For us Unity developers, maintaining our virtual environment is a never ending battle…especially when we start spawning objects:
As you can see, the Hierarchy window can fill up fast. This can make it extremely time consuming if you are attempting to troubleshoot a bug during playback when you have more than a screen full of objects to sort through.
A better way is to parent the spawned objects to a folder in the hierarchy. That way with a simple click of the triangle, you can quickly organize your objects:
The first step is to add an empty game object to your scene and reset it’s transform:
Next, we need to assign this new object as the parent. Let’s take a look at how we can accomplish this with our code.
First we need to assign the transform of our container:
And lastly we set the parent within the Instantiate argument:
It’s the last argument in the above line of code that assigns the parent of the object.
Now you can have a clutter free hierarchy!