Parent child to parent in prefab edit mode

I have an Editor Script to create AI paths, paths are made of points and links. Links are organized as children under their points. I want to turn the paths into a prefab but I am having trouble getting the script to parent links under their source points when in prefab edit mode.

Here is the creation code they all use with notes in comments:

public static GameObject CreateGameObject(string name, Transform parent, bool keepWorldPosition)
        {
            GameObject result = new GameObject(name);

            //Works to place the object in the prefab stage/scene
            StageUtility.PlaceGameObjectInCurrentStage(result); 
           
            if (parent != null)
            {               
                //Kicks the object out of the open prefab stage/scene back into the active scene as an addition to the prefab as an override
                result.transform.SetParent(parent, keepWorldPosition); 
            }
            return result;
        }

If I comment out: result.transform.SetParent(parent, keepWorldPosition); The objects are put in the prefab stage/scene under the root. I have not found a way to parent the object to other objects in the same prefab stage/scene.

Outside of the prefab edit mode, the above code works without issue.

Running:PrefabStageUtility.GetPrefabStage(parent.gameObject);
Results in successfully finding the open prefab stage, so the parent reference is most certainly the reference object inside the open prefab.

UNITY Version: 2020.3.10f1

Any help would be appreciated.

I don’t have a direct answer but I am going to guess that there is some mismatch between the new GameObject being in scene and the parent not being in scene (or vice versa).

Under normal runtime circumstances one would check the .activeInHierarchy property to make sure you can Destroy() something, eg, make sure you don’t have a prefab reference. Printing that might give you some insight, but there might be another more-applicable property for stages and the prefab editor that I’m not aware of.

If that proves to be your issue, the fix might be some kind of additional step beyond the PlaceGameObjectInCurrentStage() call to perhaps bring those two things into sync.

I was thinking along those lines as well, but from what I can tell, PrefabStageUtility.GetPrefabStageshould prove it to be the correct instance of object; such as you point out .activeInHierarchydoes for runtime objects.

I compared the results of both the parent and the object once they were in the prefab and the results are the same parent prefab object.