- Home /
How I can convert object to prefab instance?
I duplicated around 100-200 objects and then realized what just could make a prefab... But I have already configured those objects. They all include the same scripts and components. Is there any way to convert all of them to prefab instances?
Not sure I understand the question correctly, but what about just dragging them from the Scene into the Project hierarchy like you would with any other Game Object that you want to turn into a Prefab ?
Also - if all of the GameObjects are the same, then why not just create 1 Single Prefab of 1 and then use it for those 100~200 objects in the scene ?
Or if that is what you are trying to achieve then and actually just replace all of those 100~200 objects in the Scene with a Prefab then I would probably suggest to create a Prefab and then write a script that just checks your entire scene for the objects you want to replace with the Prefab and have the script do it for you, by "replacing" the GameObjects in the Scene with an instance of the Prefab.
At least that's how I would go about it
Answer by Long2904 · May 05, 2021 at 10:51 AM
So you want to convert some game objects in your scene into a prefab? Why? What did you mean when you said you had already configured those game objects? What kind of configuring did you apply to each of those objects (scaling, rotating, etc, or changing some properties of some components)? You should only use a prefab when you want to change an object in one place and then apply it to other objects. There's no built-in way for you to convert all those same objects to a prefab instance, you have to make a script that loops through all of your objects and replace it with your desired prefab (also remember to save the configuring you did on those objects). Here's an example:
// This is the component that already on your objects that you configured on
public class YourComponent : MonoBehaviour
{
public int data;
}
[ExecuteInEditMode] // This will make the script run in editor mode
public class ConvertObjectsToPrefab : MonoBehaviour
{
public GameObject desiredPrefab; // you have to make your desired prefab first and then drag it in here
public bool stop = false;
private void Update()
{
if (!stop)
{
if (desiredPrefab != null)
{
// This will find all of your objects with the type YourComponent. This is slow but idk if you have any other ways to find all the objects (does your object have a tag?)
// You can use GameObject.FindGameObjectsWithTag if your objects use the same tag
YourComponent[] allObjects = FindObjectsOfType<YourComponent>();
for (int i = 0; i < allObjects.Length; i++)
{
GameObject instance = Instantiate(desiredPrefab, allObjects[i].transform.position, allObjects[i].transform.rotation);
// This is for saving the changes that you make on each objects
instance.GetComponent<YourComponent>().data = allObjects[i].data; // if your objects have other scripts and data that you want to save then just do the same
Destroy(allObjects[i].gameObject);
}
stop = true; // prevent the code to run multiple times
}
}
}
}
Put this script on a game object, then assign the prefab that you want to replace your objects with. It will automatically run. After that, you can just remove the script.
Answer by kevinhart1001001 · May 05, 2021 at 10:06 AM
Yes - just take the object you want to make a prefab, and drag it from the hierachy into the Project folder - a new Asset should appear and you can drag it into your game :)
Maybe you misunderstood the question. The OP already had 100-200 objects that were on the scene. Drag an object to the project view only make that object into a prefab. Those 100-200 objects are still just ordinary objects in the hierarchy. Not to mention that those 100-200 has slightly different values and stuff.
Your answer
![](https://koobas.hobune.stream/wayback/20220613033050im_/https://answers.unity.com/themes/thub/images/avi.jpg)