Editor Script: Prefab instance Transform change not reflected in Scene Hierarchy

Any help on this will be much appreciated.

I have created a script to perform the following steps:

  1. Find an existing Prefab (fromPrefab)
  2. Create a new prefab and copy across the old (NewPrefab)
  3. Instantiate a Game Object from the copied prefab, in step 2.
  4. Change the Transform position on the instance (step 3).

However, the Transform in step 4 is not reflected in the Editor Scene hierarchy. The Game Object in Step 3 has a position of the Prefab created in step 2. It is not updated to the new position in step 4. ie. (9,9,9)

Some notes:

  • I have used the PrefabUtility to perform the prefab operations as documented.
  • I have assigned a new Vector 3 to the position.

Here’s the code.

    [MenuItem("TEST/TestPrefabCopy")]
    public static void TestPrefabCopy()
    {
        string[] searchInFolders = { "Assets/Prefabs" };

        // Find the prefab - Works OK
        string[] GUIDs = AssetDatabase.FindAssets("fromPrefab", searchInFolders);
        string assetPath = AssetDatabase.GUIDToAssetPath(GUIDs[0]);
        GameObject fromPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject));

        // Create a new prefab and copy from old prefab - Works OK
        string prefabPath = "Assets/Prefabs/NewPrefab.prefab";
        Object toPrefab = PrefabUtility.CreateEmptyPrefab(prefabPath);
        PrefabUtility.ReplacePrefab(fromPrefab, toPrefab, ReplacePrefabOptions.Default);

        // Instantiate Prefab - Works OK
        GameObject instantiatedObj = PrefabUtility.InstantiatePrefab(toPrefab) as GameObject;

        // THIS DOES NOT SET THE POSITION ON THE NEW PREFAB INSTANCE IN THE SCENE
        instantiatedObj.transform.position = new Vector3(9,
                                                         9,
                                                         9);
    }

I would be really grateful for help from anyone more experienced. I really don’t understand what is happening in this situation.
Thanks.

@jlear
I know this is an old question, but I recently stumbled into a similar problem.

In my case I was accidentally changing the name of the prefab prior to instantiating it. After I removed the name change code, it worked perfectly. It seems that if you change a prefab property before instantiating the prefab, it causes trouble when modifying the transform inside the scene. Don’t know why that is though.

Now looking at your code, I can not see any sign of you trying to modify the prefab property. However, I can see you are using ReplacePrefab. Try to see if removing that part works. So essentially just leave the instantiation part and the transfom position change and see if it works.

If you managed to find a solution, please do let me know. I’m really curious as to what was causing it.