- Home /
Editor randomly unparents game object after InstantiatePrefab() & SetParent() in OnInspectorGUI()
Hi,
My problem is at edit time, not runtime.
I have a monobehaviour class 'MyBe', and its CustomEditor class 'MyBeEditor'.
I have a 'MyBe' component on a game object 'MyGo' in my scene. I select 'MyGo' to display its inspector.
In MyBeEditor::OnInspectorGUI(), when pressing a GUI button:
it instantiates a gameobject using PrefabUtility.InstantiatePrefab() 'MyInst'
it sets the 'MyGo' as parent of 'MyInst' using SetParent()
So, it works sometimes ... and some other times, the 'MyInst' gets automatically unparented "somehow" from 'MyGo'. Failure rate really seems random.
Both callbacks OnTransformChildrenChanged() & OnTransformParentChanged() only seem to work at runtime, not edit time, so can't use them to see what's going wrong.
The parenting state in 'MyBe' is still correct when checking in OnValidate().
Any suggestion about what I could be missing?
Thanks,
Max
Your answer
![](https://koobas.hobune.stream/wayback/20220612002630im_/https://answers.unity.com/themes/thub/images/avi.jpg)