How to Quickly Copy Multiple Similar GameObjects into a Prefab in Unity3D

In the process of copying my game working files from one computer to the other (via SVN), I somehow lost my prefab connections.  This was a huge problem for me as I had nearly 1000 prefabs, possibly more.  Initially I started adding in replacement prefabs and copying over the position, rotation and scales of the regular gameObjects but that process was taking far too long.

Here’s a much quicker way of converting a bunch of nearly identical (except position, rotation and scale) GameObjects into prefabs.

First, create the prefab of the finalized object.  Select it and then drag the final gameObject into the Project view to create the prefab.

Create the PrefabCreate the Prefab

Create the PrefabCreate the Prefab

Select the prefab in the Hierarchy window.  The text of the prefab is blue, however the colour changes when it is selected like below.

Select the Prefab

Select the Prefab

Count the number of non-prefabbed gameObjects you’d like to replace and duplicate the existing prefab by the same number.

Duplicate the Prefab

Duplicate the Prefab

Drag each prefab into the original gameObjects to add it as a child.  Do this for each prefab and each gameObject you wish to replace.  I’d recommend expanding the non-prefabbed gameObject so you can remember which ones you have already done.

 

Parent the Prefabs

Parent the Prefabs

Now that you have the prefabs in the original GameObjects, you can quickly reset the position, rotation and scale.  Set the position and rotation all to 0 (zero) and scale to 1.  Note: The TAB key on your keyboard is definitely your friend here.  You can do each prefab in less than 3 seconds.  Do this for each of the prefabs

Reset Position, Rotation, and Scale

Reset Position, Rotation, and Scale

Select all the prefabs.  You can multiple select by holding down the CTRL key while clicking on each prefab.

Select all the Prefabs

Select all the Prefabs

Drag the selected prefabs to the location that you want them to be.  For example the parent object.

Drag the Prefabs out of their current parents

Drag the Prefabs out of their current parents

Your prefabs are now no longer children of the original gameObjects you were using.

Prefabs are unparented

Prefabs are unparented

To remove all the original non-prefab gameObjects, select them by holding down the CTRL key and clicking on each one.  You can also select the top gameObject, hold Shift and then click the bottom gameObject too.

Select the original gameObjects

Select the original gameObjects

Press the delete key on your keyboard to remove them.  You’re now left with a series of prefabs that have replaced your original gameObjects.

Converted to Prefabs

Converted to Prefabs

This all will, of course, only work for gameObjects that are identical with the exception of their position, rotation, and scale.

 

Posted in Super Space Trooper, Unity 3D, Video Game Development

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>