How can I select any gameobject/s and not only the mono script is attached to ?

I created a mono script that is attached to a cube gameobject in the hierarchy :

using System.Collections; using System.Collections.Generic;

using UnityEditor;
using UnityEngine;

public class TestMono : MonoBehaviour
{
   public GameObject SelectedObject()
   {
       return Selection.activeGameObject;
   }
}

Then created editor script inspector with a button :

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Experimental.TerrainAPI;
using UnityEngine;

[CustomEditor(typeof(TestMono))]
public class Test : Editor
{
   public override void OnInspectorGUI()
   {
       TestMono myTarget = (TestMono)target;

       if(GUILayout.Button("Test"))
       {
           myTarget.SelectedObject();
       }
   }
}

The problem is that it’s working only when selecting the cube where the mono script is attached to. But I want that if I select any gameobject in the hierarchy to get it in the script not only the gameobject it’s attach to.

Not quite sure what is it you are after. Are you saying you want to add a button to all scripts on a game object in the inspector?

If so, what it is you are trying to achieve here and what is it you want the button to do?