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?