UnityEngine.Object.Destroy not deleting GameObject

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;
		}
    }
}
1 Like

removing is not immediate with destroy but when it has time for it in the future.
if you want it to happen immediately, look at DestroyImmediate

3 Likes

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.

So something else must be going on.

iByte

oops mybad :frowning: 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).

iByte

oooa yeah such stuff can be hard to track :slight_smile:

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?

They should definitely be disappearing from the editor if destroyed. Make sure you are destroying the right thing?

I’m calling:
theObj=Instantiate()

Then calling:
Destroy(theObj,1)

They are never destroying though from the hierarchy… They do disappear from view so they are going somewhere.

Destroy will not remove anything in the editor at play time.

as the fat warning tells you, please use DestroyImmediate for “editor playtime consequences”

And ensure that you don’t call it from the object itself naturally.

1 Like

FYI, I get no “fat warning”.

I think Dreamora is referring to the manual page for Object.Destroy.

So I am back to this thread again …

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.

Hi Andeee,

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?

Thanks

iByte

classes / code is always in RAM, its not loaded on request.

the raising there are assets, potentially loaded from resources

Hi dreamora,

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.

iByte

I have similar problem, I guess,

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?

Please, don’t wake up threads that are this old. Unloading assetbundle memory is done with Unity - Scripting API: AssetBundle.Unload

You have to do:

Destroy(Obj.gameObject);

Not just:

Destroy(Obj);

In your case, you should do:

UnityEngine.Object.Destroy (level.gameObject);

Note the “gameObject” attribute of the object you want to destroy with Destroy.
And yes, that’s all you have to do.

Hope this is useful.

3 Likes

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!