Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

UnityEngine.Object.Destroy not deleting GameObject

Discussion in 'iOS and tvOS' started by ibyte, Aug 8, 2010.

  1. ibyte

    ibyte

    Joined:
    Aug 14, 2009
    Posts:
    1,050
    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?


    Code (csharp):
    1. using UnityEngine;
    2. using System;
    3.  
    4. class UnLoadGame : Action {
    5.    
    6.     public GameObject level;
    7.  
    8.     public override bool Execute (System.Object caller, Context context)
    9.     {
    10.         MasterContext masterinfo = context as MasterContext;
    11.         if (masterinfo.debug)
    12.         {
    13.             Debug.Log ("In UnLoadGame");
    14.         }
    15.  
    16.         level = GameObject.Find ("Standard Game");
    17.         if (level != null) {
    18.             UnityEngine.Object.Destroy (level);
    19.             return true;
    20.         } else {
    21.             return false;
    22.         }
    23.     }
    24. }
     
    kbearman likes this.
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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. ibyte

    ibyte

    Joined:
    Aug 14, 2009
    Posts:
    1,050
    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
     
  4. ibyte

    ibyte

    Joined:
    Aug 14, 2009
    Posts:
    1,050
    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).

    iByte
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    oooa yeah such stuff can be hard to track :)
     
  6. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    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?
     
  7. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,065
    They should definitely be disappearing from the editor if destroyed. Make sure you are destroying the right thing?
     
  8. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    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.
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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.
     
    firatlahur likes this.
  10. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    FYI, I get no "fat warning".
     
  11. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I think Dreamora is referring to the manual page for Object.Destroy.
     
  12. ibyte

    ibyte

    Joined:
    Aug 14, 2009
    Posts:
    1,050
    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.
     
  13. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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.
     
  14. ibyte

    ibyte

    Joined:
    Aug 14, 2009
    Posts:
    1,050
    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
     
  15. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    classes / code is always in RAM, its not loaded on request.

    the raising there are assets, potentially loaded from resources
     
  16. ibyte

    ibyte

    Joined:
    Aug 14, 2009
    Posts:
    1,050
    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
     
  17. AndrewK

    AndrewK

    Joined:
    Sep 14, 2012
    Posts:
    38
    I have similar problem, I guess,

    I am downloading assetbundle where I have model and material, and use
    Code (csharp):
    1. WWW.LoadFromCacheOrDownload()
    to download it. Then I instantiate this model, but after that, even if I do
    Code (csharp):
    1. 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?
     
  18. Agent_007

    Agent_007

    Joined:
    Dec 18, 2011
    Posts:
    899
  19. Delphoenyx

    Delphoenyx

    Joined:
    Jun 20, 2021
    Posts:
    4
    You have to do:
    Code (CSharp):
    1. Destroy(Obj.gameObject);
    Not just:
    Code (CSharp):
    1. Destroy(Obj);
    In your case, you should do:
    Code (CSharp):
    1. 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.
     
  20. WoodsmanThePeasant

    WoodsmanThePeasant

    Joined:
    Jun 2, 2013
    Posts:
    9
    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!
     
  21. unity_TEkEHKcvu5sRAw

    unity_TEkEHKcvu5sRAw

    Joined:
    Sep 11, 2023
    Posts:
    11
    In my case THIS was the problem! I tried to pass the variable name of my object directly into DestroyImmediate(var) instead of var.gameObject

     
    ShoniverGames likes this.