This may seem a dumb question, but how do I delete a prefab making sure it doesn’t break anything?
I have a project full of clutter and I am trying to clean it up.
I took for granted that if I delete a prefab, Unity would just replace any prefab occurrence with an unpacked version but the editor says “unpack in all open scenes”.
I thought it was just a typo, but no, it did break all prefab instances on all scenes not currently open.
So please don’t tell me again I need a script to do such a basic task.
I don’t know what to tell you man. I suppose you could do it all by hand, but I’d recommend writing some code.
https://answers.unity.com/questions/57909/find-unused-assets-in-project.html
This is well-traveled territory. There’s lots more to be read by googling for how to remove unused assets.
Yes, Unity doesn’t have a built-in dependency tracker. This free asset can get you by:
I saw Unity is working on their own package for that (finally), but I can’t find it right now.
I already have some script to check for usage inside scenes, but it is painful to:
1-Open the script editor
2-Select prefab
3-Find where it is used.
4-Open every scene where this is used and unpack.
It is a serious nightmare. I was hoping there was something built in to unpack in every scene, even closed one
I suppose I could modify my script to automatically open the scene and unpack, but if some bug in the script I may end up with some broken reference somewhere and don’t realize it immediately and maybe only discover later when I don’t even remember what that broken reference could be.
If I really need to know these things, I prefer working from the command line and I just use grep
and search the entire project for the GUID in question.
For instance, the metafile of the CapturableBase9_hexagonal_prefab.prefab:
And then I search that GUID using my grp
utility (just a convenience wrapper around the system grep
) and it will return something like this, indicating the prefab in question is used in only one other place project-wide:
The first hit is where the meta is, the second hit in CapturableBase9_all.asset is the ScriptableObject that contains that reference. Unity connects EVERYTHING via that GUID.
The nice thing about grep is that it produces a huge log you can work from if you need to exhaustively change stuff to not use a particular asset, and when you’re done, there will be no instances shown.
It is the way I use in my current script, I open the scene as a text file and look for its guid. But the problem is that it’s an undoable operation and if something is wrong I may screw up the project.
For example I discovered a bug in that script some week ago, where it wouldn’t return the occurrence of an asset if it was part of a prefab or nested prefab because the scene contain only the reference to the top most asset. If I had made a deletion based on that code, I may have deleted prefab still currently used.
That is a bug now I’m aware of and have fixed, but I cannot be aware in advance of pitfall in my code. Definitively something I wouldn’t play with. This is something that should be done on the Unity side.
If I discover the broken reference immediately, no big deal, but if it gets unnoticed it’s lost forever.
Agreed. The grep approach is really only to do that one final verification that something isn’t being used at all, which I kind of assumed was the original intent of your “can I delete this” initial post.
Assuming text serialization of assets, the default setting in Unity now, if you do not see a GUID in the project, I believe that is a 100% guarantee it isn’t directly referenced by Unity.
Don’t forget that stuff under a folder named Resources/ could always be loaded by filename! And same goes for stuff in the StreamingAssets folder