Search Unity

Question Selection Active Object nulled

Discussion in 'Editor & General Support' started by piggybank1974, May 8, 2021.

  1. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    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

    Code (CSharp):
    1.    
    2. if (Selection.activeObject != null)
    3.   {
    4.    //GameObject.Instantiate<UnityEngine.Object>(Selection.activeObject) as GameObject;
    5.     GameObject mObject = (GameObject)PrefabUtility.InstantiatePrefab(Selection.activeObject);
    6.     mObject.transform.position = new Vector3((float)Math.Round(mWorldPosition.x, 0), (float)Math.Round(mWorldPosition.y, 0), mWorldPosition.z);
    7.  
    8.       if (mParentGameObjectCurrent != null)
    9.        mObject.transform.SetParent(((GameObject)mParentGameObjectCurrent).transform);  
    10.   }
    11.  
    Cheers
     
  2. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    I came up with a workaround to the problem ditched using the Selection.Object, and here it is.

    Code (CSharp):
    1.    
    2. if (mSelectedActiveObjectIndex >= 0)
    3.    {
    4.     GameObject mTempObject = (GameObject)AssetDatabase.LoadAssetAtPath(mButtons[mSelectedActiveObjectIndex].UnityPath, typeof(GameObject));
    5.      if (mTempObject != null)
    6.        {
    7.         GameObject mObject = PrefabUtility.InstantiatePrefab(mTempObject) as GameObject;                                                                                      
    8.         mObject.transform.position = new Vector3((float)Math.Round(mWorldPosition.x, 0), (float)Math.Round(mWorldPosition.y, 0), mWorldPosition.z);
    9.  
    10.         if (mParentGameObjectCurrent != null)
    11.          mObject.transform.SetParent(((GameObject)mParentGameObjectCurrent).transform);
    12.        }
    13.    }
    14.  
     
  3. pkplonker

    pkplonker

    Joined:
    Jun 7, 2021
    Posts:
    4