Hi
My goal is to delete from a prefab a specific object. I use the following code to do so:
GameObject objectToDestroy = ...; //my input
string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(objectToDestroy);
GameObject prefabContentsRoot = PrefabUtility.LoadPrefabContents(prefabPath);
GameObject correspondingObject = ...;//Find in prefabContentsRoot the object corresponding to objectToDestroy
DestroyObject(correspondingObject );
PrefabUtility.SaveAsPrefabAsset(prefabContentsRoot, prefabPath);
PrefabUtility.UnloadPrefabContents(prefabContentsRoot);
For now, I find the corresponding object by comparing names, but this is of course not a safe way (multiple objects can have the same name in the same prefab).
I tried using the PrefabUtility.GetCorrespondingObjectXXX methods, but they return an object that is part of the prefab asset, and not part of the prefab loaded content. So I guess I need a PrefabUtility.GetCorrespondingObjectFromInstance(prefabContentsRoot, objectToDestroy), but such method does not seem to exist. How should I do?
Thanks