Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Can't destroy Transform component.

Discussion in 'Scripting' started by Reapa, Nov 8, 2013.

  1. Reapa

    Reapa

    Joined:
    May 2, 2013
    Posts:
    26
    Using C#.
    Let me first start by saying... I'm not destroying anything anywhere in my entire project that isn't a gameobject.
    I've searched all files for "GameObject.Destroy(" and the results all revealed I was only destroying gameobjects.

    So, I get the error (Can't destroy Transform component) in my console when I exit the editor play mode occasionally.

    Narrowing my problem down, it only occurs when I've spawned (instantiating a prefab) a specific object in the game (Lets call it ObjectA)... so I know it must be the cause. Though the error does not always appear with the object spawned, but it never appears when it is not spawned.

    The error can appear 1-3 times in the console (per spawned ObjectA), though I only get the error/group of errors about %50 of the time (sometimes nothing pops up).

    Lastly, I store all spawned ObjectA's inside a different container object (ContainerObjectA).

    If ObjectA is destroyed, it tells ContainerObjectA to remove it in its OnDestroy call (just before letting itself get destroyed).
    If ContainerObjectA is destroyed, it removes all ObjectA's from itself AND sets their transform.parent to null in its OnDestroy call (just before letting itself get destroyed). ObjectA is not destroyed because of removal.

    Here is an example of the code inside ContainerObjectA to remove an object.
    Code (csharp):
    1.  
    2. public void RemoveObject(ObjectA a_oObjectAToBeRemoved)
    3. {
    4.     ObjectA oObjectAMatch = null;
    5.    
    6.     foreach (ObjectA oCurrentObjectA in m_oAllObjectAs)
    7.     {
    8.         if (oCurrentObjectA == a_oObjectAToBeRemoved)
    9.         {
    10.             oObjectAMatch = oCurrentObjectA;
    11.            
    12.             break;
    13.         }
    14.     }
    15.    
    16.     if (oObjectAMatch != null)
    17.     {
    18.         oObjectAMatch.transform.parent = null; //THISLINE
    19.            
    20.         m_oAllObjectAs.Remove(oObjectAMatch);
    21.     }
    22. }
    That works fine without throwing exceptions ever. The curious thing is this... if I take out the line with comment "//THISLINE" then I will never get the error.

    If I load a new scene (effectively destroying all objects in that scene) I never get the error.
    If I spawn an ObjectA then destroy it then quit, I never get the error.


    Summary, I Get the error popping up in the console 0-3 times per ObjectA in the scene if:
    *The editor play mode is exiting
    *I set ObjectAs transform parent to null on removal (NOT destroying it)
    NOT if:
    *I'm destroying all objects by loading a new scene
    *I destroy all ObjectAs manually before exiting the editor play mode

    Sigh... can anyone shed some light?
     
    Last edited: Nov 8, 2013
  2. THoeppner

    THoeppner

    Joined:
    Oct 10, 2012
    Posts:
    205
    If you destroy an object there is no need to set the parent reference to null before.

    But I'm a little bit confused because below you're code you write

    You can't set the transform to null. Maybe there is a line like the following somewhere in your code which causes the error

    Code (csharp):
    1.  
    2. oObjectA.transform = null;
    3.  
     
  3. Reapa

    Reapa

    Joined:
    May 2, 2013
    Posts:
    26
    In this case, it is valid that ObjectA's need not be contained within ContainerObjectA as they're capable of existing independent of it. There will be cases where a container will be destroyed but the ObjectA's won't. In those cases, they will need to be un-parented.

    I miss-typed that, I've made an edit to my original post to fix it.
     
  4. nicbeanz

    nicbeanz

    Joined:
    Nov 12, 2013
    Posts:
    1
    I just had the same exact bug and I spent hours searching for the cause. By NOT putting the transform.parent = null it stopped to trigger the error at exit of the play mode in the editor.

    Sounds like a Unity Bug.

    Thank you very much for the workaround Reapa.
     
  5. noonche

    noonche

    Joined:
    Jul 7, 2013
    Posts:
    3
    I seem to be encountering the same bug. I guess it hasn't been fixed yet.
     
  6. StressedGamer

    StressedGamer

    Joined:
    Mar 15, 2015
    Posts:
    1
    I hit the same/similar problem. There is definitely something wonky when they go to clean up the graph on play mode exit.

    Turns out in my case it was because I was setting HideFlags.DontSave on the root object of the prefab but not its' children. Setting it on all children of the prefab fixed the issue for me.
     
  7. Hyperg

    Hyperg

    Joined:
    Jul 6, 2015
    Posts:
    18
    One year later (Unity 5.3.4p3) and this issue still remains. I've discovered it while making a prefab instance pool manager which has to instantiate and deactivate the objects. The transform parenting wasn't really a requirement but it would've been nice to just use transform parenting to keep track of free / used instances.
     
  8. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    391
    In my case the culprit were HideFlags: parent object was marked with HideFlags.DontSave and it was causing the error on exiting play mode. After I've removed flags the error disappeared.
     
    Fewes and LabOSM like this.
  9. fonko

    fonko

    Joined:
    Mar 20, 2014
    Posts:
    23
    looks like if you create a gameobject over your start() function forgetting that you have a prefab already into your project and some how (probably very high on thc) there is also a reference "public prefabToInstantiate" (or similar... not sure... ) in your script... then unity throws this error.... also you're probably using this object to be parent of a bunch of game objects created with "Instantiate(prefab,parent.transform) being parent.transform the problematic gameobject!

    look up for "public prefab;" sort of code, probably from a prefab you've referenced through the editor.

    check for declarations of the same prefab/variable like:

    Code (CSharp):
    1. platformHolder = new GameObject("Platform Holder");  
    make up your mind how you want that object to be created, pick only one way to do it , pothead! :)

    ...that was what caused me this headache!
     
  10. gzx

    gzx

    Joined:
    Oct 17, 2018
    Posts:
    2
    I had the same problem. Seems the bug still remains in Unity 2018.2.
    Here is my workaround:
    Don't set transform.parent=null inside OnDestroy(). just make a function like DetachChildAndDestory() where you first set the parents of children to null, then you destroy the gameobject.
    Then you can just call DetachChildAndDestory() instead Destroy() when you want to destroy the container.
     
  11. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,933
  12. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    212
    @Elringus - OMG thanks for that piece of information! I suffered from this error for literally years and now finally managed to fix it. It was indeed the hideflags being different in parent and children. Instead of just setting it on the parent and assuming Unity can handle this, I now run this (rather more expensive code) and am rid of the problem!


    Code (CSharp):
    1.  
    2.     public static void SetHideFlagsRecursively(this Component obj, HideFlags flags)
    3.     {
    4.         foreach (var child in obj.GetComponentsInChildren<Transform>(true))
    5.         {
    6.             child.gameObject.hideFlags = flags;
    7.         }
    8.     }
    9.  
     
  13. Captain_Flaush

    Captain_Flaush

    Joined:
    Apr 20, 2017
    Posts:
    46
    OMFG... Had this bug for one year.
    Just removed the flags from the parent of the spawned objects and it got fixed.
    God bless this post.
     
    LabOSM likes this.
  14. 2782119390

    2782119390

    Joined:
    Jul 31, 2019
    Posts:
    1
    hello,i have a same problem when making object pool.Have you solved this?
     
  15. Bananozuar

    Bananozuar

    Joined:
    Oct 15, 2016
    Posts:
    3
    Late reply, but maybe someone else will find it usefull.
    I found thath calling transform when object was getting destroyed poped up this error. I just put one if and it's gone.,

    Code (CSharp):
    1. if(gameObject.activeInHierarchy) {
    2.            transform.SetParent(null);
    3. }
     
unityunity