- Home /
How can I remove an object from an asset?
I can call AssetDatabase.AddObjectToAsset to add something to a prefab. How can I remove one part but leave the rest in tact and still "connected" to clones that might be using the remaining stuff inside?
According to here you can overwrite the file, but indexes might be messed up. Ideally there would be an AssetDatabase.RemoveObjectFromAsset type of call.
Answer by jspease · Jun 15, 2012 at 05:00 AM
You can simply call UnityEngine.Object.DestroyImmediate(objectInAsset, true)
. You also need to hit Save in the editor after doing this (calling AssetDatabase.SaveAssets()
might work too) for the change to take effect.
At least, I have used this successfully when dealing with many separately-referenced meshes in one .mesh file. In case it matters, I was using Unity 3.5.2 and the project was set to store all assets in text format.
You may also need AssetDatabase.ImportAsset(mainAsset), as is recommended after calling AddObjectToAsset.
This does not work for me. When I Debug.Log(object) I see it's null but it persists under the asset in project tab. Somehow it doesn't get destroyed.
Your answer
Follow this Question
Related Questions
Can I safely put a prefab asset into version control with Unity Free? 3 Answers
A event on deletion of asset in the asset database? 1 Answer
Could not load copied asset via AssetDatabase.LoadAssetAtPath 4 Answers
Need to make a reference in a prefab to itself not to the created instance 2 Answers
Seperating editor and runtime data with ScriptableObjects 1 Answer