This can be easily done, manually, in a variety of ways. Unfortunately, you report having 100 game objects that you wish to exchange, in this way. Rather than manually completing the task, let us write a script to do it.
Note that this is still specific to the editor. Using the namespace UnityEditor
, you have the ability to manipulate the editor, which allows you to complete repetitive tasks such as this one.
This is a stand alone script that you can attach to any game object, in order to complete the described task with less repetition. It allows you to replace game objects en mass, by either storing them into an array, or by using tags to automatically construct an array.
Note that you specifically mention position. I also take rotation in to account, as well as potential parenting. If you do not wish to include rotation, simply use Quaternion.identity
for the rotation value. If you do not wish to include parenting, simply remove the if
statement that handles it.
using UnityEngine; // UnityEngine gives us general access.
using UnityEditor; // UnityEditor gives us editor-specific access.
/// <summary>Performs manual iteration to swap out one game object for another.</summary>
public class PrefabSwitch : MonoBehaviour
{
/// <summary>The new object to instantiate in place of the old object.</summary>
public GameObject newPrefab;
/// <summary>The old objects, intended to be swapped out for iterations of
/// the new object.</summary>
public GameObject[] oldGameObjects;
/// <summary>The string tag to use when replacing objects by tag.</summary>
public string searchByTag;
/// <summary>Swaps all the game objects in oldGameObjects for
/// a new newPrefab.</summary>
public void SwapAllByArray()
{
// Store a boolean to detect if we intend to swap this game object.
bool swappingSelf = false;
// For each game object in the oldGameObjects array,
for (int i = 0; i < oldGameObjects.Length; i++)
{
// If the current game object is this game object,
if (oldGameObjects[i] == gameObject)
{
// Enable the flag to swap this game object at the end, so we
// do not destroy it before the script an complete its task.
swappingSelf = true;
}
else
{
// Else, we are not dealing with the game object local to this
// script, so we can swap the prefabs, immediately.
SwapPrefabs(oldGameObjects[i]);
}
}
// If we have flagged the local game object to be swapped,
if (swappingSelf)
{
// Swap the local game object.
SwapPrefabs(gameObject);
}
}
/// <summary>Swaps all the game objects that use the tag <code>searchByTag</code>.
/// If empty, we will use the tag of the local game object.</summary>
public void SwapAllByTag()
{
// If searchByTag is null,
if (searchByTag == "")
{
// Set searchByTag to the tag of the local game object.
searchByTag = gameObject.tag;
}
// Find all the game objects using the tag searchByTag,
// store them in our array, and proceed to swapping them.
oldGameObjects = GameObject.FindGameObjectsWithTag(searchByTag);
SwapAllByArray();
}
/// <summary>Swaps the desired oldGameObject for a newPrefab.</summary>
/// <param name="oldGameObject">The old game object.</param>
void SwapPrefabs(GameObject oldGameObject)
{
// Determine the rotation and position values of the old game object.
// Replace rotation with Quaternion.identity if you do not wish to keep rotation.
Quaternion rotation = oldGameObject.transform.rotation;
Vector3 position = oldGameObject.transform.position;
// Instantiate the new game object at the old game objects position and rotation.
GameObject newGameObject = Instantiate(newPrefab, position, rotation);
// If the old game object has a valid parent transform,
// (You can remove this entire if statement if you do not wish to ensure your
// new game object does not keep the parent of the old game object.
if (oldGameObject.transform.parent != null)
{
// Set the new game object parent as the old game objects parent.
newGameObject.transform.SetParent(oldGameObject.transform.parent);
}
// Destroy the old game object, immediately, so it takes effect in the editor.
DestroyImmediate(oldGameObject);
}
}
/// <summary>Custom Editor for our PrefabSwitch script, to allow us to perform actions
/// from the editor.</summary>
[CustomEditor(typeof(PrefabSwitch))]
public class PrefabSwitchEditor : Editor
{
/// <summary>Calls on drawing the GUI for the inspector.</summary>
public override void OnInspectorGUI()
{
// Draw the default inspector.
DrawDefaultInspector();
// Grab a reference to the target script, so we can identify it as a
// PrefabSwitch, instead of a simple Object.
PrefabSwitch prefabSwitch = (PrefabSwitch)target;
// Create a Button for "Swap By Tag",
if(GUILayout.Button("Swap By Tag"))
{
// if it is clicked, call the SwapAllByTag method from prefabSwitch.
prefabSwitch.SwapAllByTag();
}
// Create a Button for "Swap By Array",
if (GUILayout.Button("Swap By Array"))
{
// if it is clicked, call the SwapAllByArray method from prefabSwitch.
prefabSwitch.SwapAllByArray();
}
}
}
Replace By Tag
Replacing game objects by tag seems to be the most viable option, short of manually selecting the group of game objects you wish to swap out. If the set of game objects you are swapping out are all of the same prefab, replacing by tag is even easier.
First, you want to make sure that all of the targeted objects have the same tag. If needs be, you can make a temporary tag, to specifically mark the objects you wish to swap. You can apply this tag to the prefab of the old objects, and simply update it to impact all of them. Keep in mind that only the game objects you intend to swap should use this tag; the script will target all game objects using the given tag.

Replace By Array
Replacing by array still requires a small amount of manual work, but allows you to quickly select a group of game objects, and immediately swap them out. This method is especially useful, as it allows you to quickly select the desired group, and is more reliable in that you confirm exactly which objects will be affected.
Note that you can select a group of objects and drag them on to a public array, to add them to the list as shown, below. If you drag additional objects on to the array in the same way, where there are already objects in the array, the new objects will simply be added to the array, along with the original objects that were already there.
Also note that you can click on the padlock icon, at the top right corner of the inspector, to lock that view. If you do not do this, selecting your group of old game objects will take focus away from the selected object. This is especially annoying when the selected object was the container for your script, and in taking focus away, you are unable to drop your selection on to the scripts public array.
