Hi all,
I’ve managed to reduce my problem case to a very simple example. I have a GameObject at the root of my scene with the following component attached:
[ExecuteInEditMode]
public class ParentComponent : MonoBehaviour
{
void Awake()
{
GameObject child = new GameObject("Child");
child.transform.parent = transform;
child.hideFlags = HideFlags.DontSave;
}
}
So my GameObject creates a child when it is initialized, and because it is created automatically I don’t want to save it with the scene. In my real-world scenario this is used as the basic for creating a hierarchy of runtime-generated meshes.
As expected, the child object is not saved. However, the parent does still save a reference to the child object. This is a problem because it then tries to load the child object when it loads the scene, and of course the child object doesn’t exist. Hence the following error is given:
“CheckConsistency: Transform child can’t be loaded”
It seems to me that Unity should not save a reference to an object when it is not going to save the object itself. Yet it clearly does, because the saved scene looks like this:
.
.
m_Children:
- {fileID: 0}
.
.
Whereas if it truly has no children then the saved scene looks like this:
.
.
m_Children: []
.
.
Has anyone else encountered this, and how did you get around it? One possible solution would be to break the parent-child relationship before serialization occurs. I tried:
void OnDisable()
{
child.transform.parent = null;
}
But was told:
“Cannot change GameObject hierarchy while activating or deactivating the parent.”
I also tried breaking the parent-child relationship in OnDestroy(), but it seems serialization has already occurred by this point.
Any thoughts are welcome, otherwise I will report this as a bug.