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.