Can't destroy Transform component.

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.

public void RemoveObject(ObjectA a_oObjectAToBeRemoved)
{
	ObjectA oObjectAMatch = null;
	
	foreach (ObjectA oCurrentObjectA in m_oAllObjectAs)
	{
		if (oCurrentObjectA == a_oObjectAToBeRemoved)
		{
			oObjectAMatch = oCurrentObjectA;
			
			break;
		}
	}
	
	if (oObjectAMatch != null)
	{
		oObjectAMatch.transform.parent = null; //THISLINE
			
		m_oAllObjectAs.Remove(oObjectAMatch);
	}
}

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?

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

oObjectA.transform = null;

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.

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.

I seem to be encountering the same bug. I guess it hasn’t been fixed yet.

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.

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.

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.

2 Likes

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:

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! :slight_smile:

…that was what caused me this headache!

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.

Transform

@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!

    public static void SetHideFlagsRecursively(this Component obj, HideFlags flags)
    {
        foreach (var child in obj.GetComponentsInChildren<Transform>(true))
        {
            child.gameObject.hideFlags = flags;
        }
    }
4 Likes

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.

1 Like

hello,i have a same problem when making object pool.Have you solved this?

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.,

if(gameObject.activeInHierarchy) {
           transform.SetParent(null);
}
1 Like

Bug still there 9 years after first report. @Bananozuar1 's solution worked for me.

Someone said you don’t have to set the parent to null before destroying it, but actually it is a good idea. Otherwise the parent’s childCount remains unchanged and other code might still try to reference the destroyed child.