Any help on this will be much appreciated.
I have created a script to perform the following steps:
- Find an existing Prefab (fromPrefab)
- Create a new prefab and copy across the old (NewPrefab)
- Instantiate a Game Object from the copied prefab, in step 2.
- 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.