Replace game object with prefab?

I know I can replace a prefab with a game object, but can I do the reverse? Meaning, I want to keep the same settings, at LEAST the transform, of the GO, but replace it with a prefab.

Would make importing game objects to other scenes a little more do-able.

Gotta be a way to script that.

4 Likes

If I’m not mistaken, this is actually default behaviour. A prefab instance always has its transform settings as “overriden”. So you can move the instance around, then change the prefab, and the instance will take on all settings from the prefab, except its transform settings.

Bye, Lucas

I should have been more clear.

I don’t want to change settings on the prefab. I want to REPLACE the ENTIRE prefab with another.

Example:
I make a nice particle effect. I place this in the scene in several places. Then, my partner makes a better effect on his computer. I want to replace my particles with his, so I copy his prefab to my project.

But now, there is no way for me (that I know of!) to replace my prefab with his.

Just drag an instance of his prefab from the scene hierarchy to the project pane, overriding your existing prefab.

Thanks.

Now the hard part: say you have a game object that contains one or more objects that you want to replace with prefabs. Think of them as place-holder objects. Now I want to replace the place-holders with prefabs, but without having to copy/paste all the params around. Is there a way to do that?

1 Like

Hey,
i had the same problem and wrote a little script to fix it.
Its realy not that hard as I thougt ^^

What it does:
The script lets you replace a hole bunch of placeholders at the same by an predefined Prefab.
The object you put in for replacing needs to be a Prefab.

If you want to replace many placeholders at the same time, attach them as children to another Object. For example an
Empty. Then declare this Parent as the object to replace.
The script will automaticly check for its children an replace them.

Feel free to do some improvements on the script. But please be fair enough to share it.
So others could benefit of it.

4 Likes

INSTANCE REPLACEMENT:

Drag your new Prefab onto the existing GameObject in the Hierarchy while holding [Alt].

40 Likes

Hi

I changed the script to allow for changing any number of gameobjects with a specific prefab.
It also keeps the originals parents transform

using UnityEngine;
using UnityEditor;
using System.Collections;
// CopyComponents - by Michael L. Croswell for Colorado Game Coders, LLC
// March 2010

public class ReplaceGameObjects : ScriptableWizard
{
    public bool copyValues = true;
    public GameObject NewType;
    public GameObject[] OldObjects;

    [MenuItem("Custom/Replace GameObjects")]


    static void CreateWizard()
    {
        ScriptableWizard.DisplayWizard("Replace GameObjects", typeof(ReplaceGameObjects), "Replace");
    }

    void OnWizardCreate()
    {
        //Transform[] Replaces;
        //Replaces = Replace.GetComponentsInChildren<Transform>();

        foreach (GameObject go in OldObjects)
        {
            GameObject newObject;
            newObject = (GameObject)EditorUtility.InstantiatePrefab(NewType);
            newObject.transform.position = go.transform.position;
            newObject.transform.rotation = go.transform.rotation;
            newObject.transform.parent = go.transform.parent;

            DestroyImmediate(go);

        }

    }
}
13 Likes

Great script… thanks…

Many thanks. It has safe me a lot of work hours

Thank you Kudos, this should be a standard Unity function imho…

5 Likes

Heya guys,
I’m having a hard time trying to get this to work in my situation. The alt drag doesn’t work for either. It says that this is not implemented yet. As for the script it says it can’t find the prefab in the prefab library. any ideas?

Hey, I just registered on the community to say thank you to sunset and kudosforludos for this script. It totally worked and saved me a lot of bother :slight_smile:

Excellent script. I modified it slightly so it copies scale and it prefills the selection

using UnityEngine;
using UnityEditor;
using System.Collections;

// CopyComponents - by Michael L. Croswell for Colorado Game Coders, LLC
// March 2010

//Modified by Kristian Helle Jespersen
//June 2011

public class ReplaceGameObjects : ScriptableWizard
{
    public bool copyValues = true;
    public GameObject NewType;
    public GameObject[] OldObjects;

    [MenuItem("Custom/Replace GameObjects")]
    static void CreateWizard()
    {
        var replaceGameObjects = ScriptableWizard.DisplayWizard <ReplaceGameObjects>("Replace GameObjects", "Replace");
        replaceGameObjects.OldObjects = Selection.gameObjects;
    }

    void OnWizardCreate()
    {
        //Transform[] Replaces;
        //Replaces = Replace.GetComponentsInChildren<Transform>();

        foreach (GameObject go in OldObjects)
        {
            GameObject newObject;
            newObject = (GameObject)EditorUtility.InstantiatePrefab(NewType);
            newObject.transform.parent = go.transform.parent;
            newObject.transform.localPosition = go.transform.localPosition;
            newObject.transform.localRotation = go.transform.localRotation;
            newObject.transform.localScale = go.transform.localScale;

            DestroyImmediate(go);
        }
    }

}
2 Likes

Same Here (3.5.2) - Alt-dragging a prefab from Project into a different prefab in Hierachy does not swap in the new prefab. It does nothing. (And occasionally crashes.) I don’t need the script and didn’t try it, but dragging manually would be great!

I know this is an old thread but thanks ALOT, because this is saving me a lot of work (and nerves).

im totally noob in this…
how to apply this script on my prefab ?

Yeah, sorry guys, I also don’t know how to make this work. I’ve created a new script (CopyComponents.cs), and the Custom > ReplaceGameObjects menu shows up…but when I try to use the option, I get a “NullReferenceException: Object reference not set to an instance of an object” error. What am I doing wrong?

To the people that can’t seem to get this to work. Make sure you scrips file name matches the class name.

Thank you !