Swapping Places

In my recent work in Unity freelancing, I found a need to turn a rapid prototype of a game into a fully working viable product, a lot of the original code was unusable and was not designed to be scaled or reused in any way. Very little thought was given to expanding on the prototype and the project was threatened with development hell if the problem of scalability and maintainability was not solved soon.
This involved creating entirely new classes to organize objects and behaviors as well as widespread use of Unity’s prefab system which for some reason the prototype had no prefabs used at all and all of the objects were just cut and pasted throughout the scene with no way to maintain or update them. For instance if I wanted to add another script to all of a specific type of “enemy” object I would have to manually go into each scene and see if there was a copy of the object and question and manually update it. Simply put this would be a total mess and time consuming and soul draining work.

This mess turned out to be easier to solve than I originally thought, first I created new prefabs for all the objects that needed replaced and then I wrote a small script that would instantiate the prefab with the same position and rotation as the original object. By doing this I could select large amount of objects in the Hierarchy panel and unity and quickly replace them without any hassle.

Here is the script that I used, when placed in a unity project the script should show under a header as “Custom -. Replace Objects”, just select objects in the Hierarchy panel that are going to be replaced and select the replace objects and a modal will appear asking for the prefab that will replace the selected objects, simple, yet effective.

using UnityEngine;
using UnityEditor;

public class ReplaceGameObjects : ScriptableWizard
	//Prefab that will replace objects
    public GameObject Prefab;
	//List of objects that will get replaced by the above prefab
    public GameObject[] ObjectsToReplace;
	//Keep the original object names of objects getting replaced
    public bool KeepOriginalNames = false;
    [MenuItem("Custom/Replace GameObjects")]
    static void CreateWizard()
		//Create the script UI in Unity
        var replaceGameObjects = DisplayWizard<ReplaceGameObjects>("Replace GameObjects", "Replace");
        replaceGameObjects.ObjectsToReplace = Selection.gameObjects;
    void OnWizardCreate()
        foreach (GameObject go in ObjectsToReplace)
			//Create new gameobject and set it to the same position and rotations as the old object
            GameObject newObject;
            newObject = (GameObject)PrefabUtility.InstantiatePrefab(Prefab);
            newObject.transform.SetParent(go.transform.parent, true);
            newObject.transform.localPosition = go.transform.localPosition;
            newObject.transform.localRotation = go.transform.localRotation;
            newObject.transform.localScale = go.transform.localScale;
            if (KeepOriginalNames)
                newObject.transform.name = go.transform.name;
			//Destroy original object