I have a gameObject that is a placeholder for all the other gameObjects in a level. When I want to unload the level I am trying to delete that gameObject. Via the debugger I see it calls UnityEngine.Object.Destroy but the object and all it’s children are not removed.
What should I be looking for?
using UnityEngine;
using System;
class UnLoadGame : Action {
public GameObject level;
public override bool Execute (System.Object caller, Context context)
{
MasterContext masterinfo = context as MasterContext;
if (masterinfo.debug)
{
Debug.Log ("In UnLoadGame");
}
level = GameObject.Find ("Standard Game");
if (level != null) {
UnityEngine.Object.Destroy (level);
return true;
} else {
return false;
}
}
}
Hi Dreamora, I have used Destroy successfully before and the effect (at least at the time was immediate). I tried DestroyImmediate to see if that made a difference in this case and the GameObject still is not destroyed.
oops mybad it was deleting then going right back in to a recreate state that i didn’t see it disappear and then come back. I renamed a item parented under that game object while running and it is indeed deleted (and then quickly recreated).
Does the editor not clean up things by the way? I am creating instances of an object every time something blows up and end up with hundreds of instances in my editor list. I am calling Destroy but they still appear in my Hierarchy. Are they just ghosts?
My code does a LoadLevelAdditive - all GameObject’s for the level are parented under a placeholder game object to make to easy to nuke them later …
While I see Unity is destroying the parent game Object and all the children (as viewed in the editor) … I can see through instruments on the actual device that the 10MB of memory used by my level is not being freed up and every time I reload the level another 10MB is consumed.
So first of all am I handling this correctly? I am probably being a little lazy here trying to flush the toilet vs picking out the poo but i really would like to reset all elements of the level to a known state easily if possible.
iByte
EDIT: reread the thread, will try destroy immediate again but I don’t think that should be necessary?
RE-EDIT: DestroyImmediate did not seem to make any difference.
Does it seem as though the memory is being leaked (ie, it gets used up irreversibly until it eventually leads to a crash) or is it just not being reclaimed immediately? Generally, blocks of memory that allocated will be recovered by the garbage collector in the fullness of time.
for a start I decided to rule out any external code being called via plugins and it turns out my own plugin is the main culprit.
Having said that I would still appreciate your interpretation of this allocation memory graph I to took with all external “Obj-C” code disabled.
What could be in the first big memory allocation when the level is loaded the first time but not on subsequent loadLevelAdditive calls?
If code in my level references a class like iTween for example - would that class memory be released when the level is unloaded or would that stick around?
So the memory delta between load and unload is GameObject memory.
At this point having the assets stay around is okay as I am always reusing most of them but if one wanted to release the memory used by loaded assets how would that be accomplished? I am not using asset bundles.
I am downloading assetbundle where I have model and material, and use WWW.LoadFromCacheOrDownload() to download it. Then I instantiate this model, but after that, even if I do Object.DestroyImmediate(gameObject); and download again this model (now from cache) and instantiate it again my RAM allocation increases.
What can I do with this? How can I frees up the memory from this deleted gameObjects?
If anyone else comes across this and doesn’t find a solution, I had this issue because I was attempting to delete a list element. I saved the object I was trying to delete as a local variable and then destroyed the local variable and it destroyed the list item just the same. If your object isn’t being destroyed, maybe you are referencing it from a list, and if so, save it as a local variable and destroy the local variable. Hope that helps someone!