Selection Active Object nulled

Hi All,

I’m in the process of creating a prefab plotter editor window, after today I’ve got a good part of it working, to some degree, but I’m having the following problem.

My editor window from a combobox populates a series of buttons that represent prefabs that it finds in a directory, once I press a button it loads the prefab in using AssetDatabase.LoadAssetAtPath and then I use the editors Selection.activeObject to make this the active selection.

I set up an event that interacts with the SceneView using the SceneView.duringSceneGui event.

now once I left-click the scene view it places the gameobject at the given position. but then it automatically resets “null” the Selection.activeObject object, meaning I have to click the editor button again.

I’m using PrefabUtility.InstantiatePrefab to create the prefab, but If I use an ordinally GameObject.Instantiate it does NOT reset the Selection.activeObject, I’m I using it incorrectly or is this a bug? I’m using Editor 2021.1.5F1

if (Selection.activeObject != null)
  {
   //GameObject.Instantiate<UnityEngine.Object>(Selection.activeObject) as GameObject;
    GameObject mObject = (GameObject)PrefabUtility.InstantiatePrefab(Selection.activeObject);
    mObject.transform.position = new Vector3((float)Math.Round(mWorldPosition.x, 0), (float)Math.Round(mWorldPosition.y, 0), mWorldPosition.z);

      if (mParentGameObjectCurrent != null)
       mObject.transform.SetParent(((GameObject)mParentGameObjectCurrent).transform);   
  }

Cheers

I came up with a workaround to the problem ditched using the Selection.Object, and here it is.

if (mSelectedActiveObjectIndex >= 0)
   {
    GameObject mTempObject = (GameObject)AssetDatabase.LoadAssetAtPath(mButtons[mSelectedActiveObjectIndex].UnityPath, typeof(GameObject));
     if (mTempObject != null)
       {
        GameObject mObject = PrefabUtility.InstantiatePrefab(mTempObject) as GameObject;                                                                                      
        mObject.transform.position = new Vector3((float)Math.Round(mWorldPosition.x, 0), (float)Math.Round(mWorldPosition.y, 0), mWorldPosition.z);

        if (mParentGameObjectCurrent != null)
         mObject.transform.SetParent(((GameObject)mParentGameObjectCurrent).transform);
       }
   }

I believe you were looking Unity - Scripting API: Selection.transforms

Appreciate this won’t help you any more, but if anyone stumbles across this in future.

I wanted to pass a transform in, so used this " Selection.transforms.Length!=0? Selection.transforms[0]:null