I’m writing a custom Editor script to help be build my world. As part of that, my script runs through a list of Prefabs and adds them randomly into the Hierarchy. This is done in Edit mode, not runtime.
When instantiating the random Prefabs into my game Hierarchy, it of course creates a new GameObject copy based on the prefab. Instead, I want my script to place the prefab itself into the Hierarchy window. How do I go about doing this?
// Assign prefab in Inspector
public GameObject somePrefab;
// Get the Path to Prefab
string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(somePrefab);
// Load Prefab Asset as Object from path
Object _newObject = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(Object));
//Instantiate the Prefab in the scene, as a child of the GO this script runs on
GameObject _newPrefabInstance = PrefabUtility.InstantiatePrefab(_newObject, this.transform) as GameObject;