I have a enemy prefab,that has a script adding animation clips at Start().
I Instantiate the prefab,and when it is “dead”,I want to destroy it.
But I found that though the item in Hierarchy is disappeared when run at editor, the memory do not free when I built to run on Ipod.
I have tried to remove all clips before Destroy or DestroyImmediate.But it seems not work.
I have had a test that:
If I comment the addClip functions at the Start(), the memory can be free.
So I guess that the clip used some assets and it prevent the Destroy to free the memory.But the removeClip seems not work. I also try to delete the animation Component,it also not work.
What are you using as the parameter for the Destroy function? You should use the gameObject property of the script rather than the script itself:-
Destroy(gameObject);
If you try to do something like:-
Destroy(this);
…then it will destroy the script component rather than the whole object. Also, depending on what you are doing, you may need to call Resources.UnloadUnusedAssets after destroying the object to actually reclaim the memory.
Thanks for your reply.
I called the Destroy with GameObject parameter,
As I metioned above, after comment the AddClip calls in Start(),the memory is freed properly.
So I have thought about unload unused assets,but have no idea to do it.
I have not tried the Resources.UnloadUnusedAssets , I will try it later.
thanks for your advice.
Seems the UnloadUnusedAssets() does some work, but not very nice.
I changed the way of adding clip by adding clip in editor, it seems not a good way of development,but the memory can be free properly in this way.