- Home /
Does UnityEngine.Object.DestroyImmediate() invoke PrefabUtility.PrefabInstanceUpdated?
Hi All,
Does UnityEngine.Object.DestroyImmediate(*someObject*, true)
invoke the PrefabUtility.PrefabInstanceUpdated
MulticastDelegate?
Thanks,
Lee
This is the actual DestroyImmediate functions code
/// <summary>
/// <para>Destroys the object obj immediately.</para>
/// </summary>
/// <param name="obj">Object to be destroyed.</param>
/// <param name="allowDestroyingAssets">Set to true to allow assets to be destoyed.</param>
[$$anonymous$$ethodImpl($$anonymous$$ethodImplOptions.InternalCall)]
public static extern void DestroyImmediate(Object obj, [DefaultValue("false")] bool allowDestroyingAssets);
There doesn't seem to be any call to PrefabUtility.PrefabInstanceUpdated
.
That conclusion makes no sense. DestroyImmediate is an "extern" method, so it's code is defined in the native part of the editor. We can not know what exactly happens over there.
However the question is not really clear what "someObject" actually is. "PrefabUtility.PrefabInstanceUpdated" is usually called when you apply changes to the actual prefab. Destroying an instance of a prefab does not update / change the prefab and therefore the callback won't be called. If "someObject" is the actual prefab, it might be executed but i actually doubt it.
Sorry, I kind of new my conclusion didn't make much sense. But, as you say, it is an "extern" method and we don't know what's in there. So, there's no way of knowing whether PrefabUtility.PrefabInstanceUpdated
is called from inside DestroyImmediate or not.
Hi @aditya007! I can't see the function body so I can't really be certain whether or not it invokes the PrefabInstanceUpdated
delegate. However, even if it doesn't there isn't a guarantee that event doesn't spawn the update in another part of the editor. Does that make sense or am I talking out of my a$$?
I haven't really used the PrefabInstanceUpdated delegate (and i'm sure 99.9% of all Unity developers haven't either). I do a lot editor scripting, but i don't work that much with prefabs. I do alot things procedurally ^^.
By a quick google and looking at some usecases, it seems that PrefabInstanceUpdated takes a prefab instance as parameter. The instance where you pressed "apply" on. So removing the prefab itself just breaks the prefab connection. If Unity would call PrefabInstanceUpdated it would need to call it for every instance of that prefab, which doesn't make any sense.
It's still not clear what actual problem you try to solve. If you want to get a callback when a prefab is destroyed, use an Asset$$anonymous$$odificationProcessor. You can also use an AssetPostprocessor, though that callback is called after the asset has been deleted, so at this point it's already gone. It's useful to clean up some internal state of your scripts when an asset got deleted.
Again, sorry for the lack of clarity @Bunny83. I'm adding ScriptableObject
assets as sub-assets of a prefab... I know, don't say it. You ever start trying to solve something and refuse to give up until you get it to work even when it's a bad idea? I think that's my superpower....
As I cannot find a suitable way to remove the subassets (`DestroyImmediate(subAssetObject, true);` doesn't remove the subasset) I was hoping to replace the prefab (also didn't work) and ended up destroying the prefab asset, recreating it, and then rebuilding it from the ground up (also, doesn't remove the subassets).
I'm getting the delegate call on all instances and when I check:
if (((GameObject)PrefabUtility.GetPrefabParent(instance) == instance)
{
Debug.Log("Oh snap!");
}
... I see "Oh snap!" print to the ter$$anonymous$$al making me think it's invoked on the prefab as well.
Thank you for pointing me toward Asset$$anonymous$$odificationProcessor it looks like it will solve my problem.
Sincerely,
Your answer
![](https://koobas.hobune.stream/wayback/20220612110301im_/https://answers.unity.com/themes/thub/images/avi.jpg)