Hi,
I’d like to clone a prefab that is already in the scene as a prefab.
var go = PrefabUtility.InstantiatePrefab(scene_prefab) as GameObject;
That produce a null gameobject. I tried gethandleof… and others, but that does not work anyway.
I’m doing anything wrong? Never had this issue.
Thank you…
1 Like
This may help
Not pretty but it works (tested in 2019.3).
Selection.activeGameObject = someGameObjectToCopy;
Unsupported.CopyGameObjectsToPasteboard();
Unsupported.PasteGameObjectsFromPasteboard();
var copy = Selection.activeGameObject;
Edit : Necro-posting as this was unanswered and while searching in Google this was one of the first results. Better to have answers than having to keep looking for them.
halley
January 12, 2024, 2:36am
3
Edit: Oh, a necro-post.
I think you’re looking for PrefabUtility.GetPrefabObject().
GameObject prefabAsset = PrefabUtility.GetPrefabObject(scene_object_which_may_be_part_of_prefab_instance);
GameObject instance;
if (prefabAsset != null)
instance = PrefabUtility.InstantiatePrefab(prefabAsset);
else
instance = Instantiate(scene_object_which_may_be_part_of_prefab_instance);