I’m trying to create a “preview” object for a custom inspector but I get this error. It happens when I open Unity (with this project) but only if I had this test object selected and hit “save” when Unity asked to save the scene when closing Unity. It also happens if I had the object selected when I hit Play. Is there anything I’m missing here? It seems I must manually destroy the object and that is why I’ve added the code in OnDisable() and OnDestroy() but they don’t seem to be called when I need it.
[edit] I should point out that if I don’t parent the preview object then it is all fine, but I would obvioulsy like it to be moved around with the selected object (therefore the part where I make it a child).
CheckConsistency: Transform child can't be loaded
Error in file: .... Transform.cpp line 1292
This is the code…
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(SpawnPoint))]
public class SpawnPointInspector : InspectorBase<SpawnPoint>
{
private GameObject preview = null;
void OnEnable()
{
if (preview == null Target.actorDef != null)
{
if (Target.actorDef.art)
{
preview = GameObject.Instantiate(Target.actorDef.art) as GameObject;
preview.transform.parent = Target.transform;
preview.transform.localPosition = Vector3.zero;
HideAndDontSave(preview);
}
}
}
void OnDisable()
{
if (preview != null)
{
GameObject.DestroyImmediate(preview);
preview = null;
}
}
void OnDestroy()
{
if (preview != null)
{
GameObject.DestroyImmediate(preview);
preview = null;
}
}
private void HideAndDontSave(GameObject go)
{
go.tag = "EditorOnly";
go.hideFlags = HideFlags.HideAndDontSave | HideFlags.HideInInspector | HideFlags.NotEditable;
if (go.transform.childCount > 0)
{
for (int i = 0; i < go.transform.childCount; i++) HideAndDontSave(go.transform.GetChild(i).gameObject);
}
}
}
[edit2] I’ve decided to not parent it and just do this, but I would like to know if I where doing something wrong in my code above or missed something or could have done something better.
void OnSceneGUI()
{
if (preview != null)
{
preview.transform.position = Target.transform.position;
}
}