How make EditorGUILayout.ObjectField get a Monobehaviour from the Assets folder

In a custom editor, I want to let the user select a monobehaviour script from the Assets folder. Unfortunately, I’m not having much luck.

The code I’m using is:

 script = EditorGUILayout.ObjectField(script, typeof(MonoBehaviour));

It creates the field, but only let me select GameObjects in the scene.

So, anyone knows how to use the ObjectField to select a monobehaviour from the assets?

A MonoBehaviour script asset is not the same as the MonoBehaviour component. What you need is the MonoScript class. The MonoScript class is basically just a TextAsset which contains the source code, but in addition it has the GetClass function which can be used to access the “MonoBehaviour-type” that is implemented by this script. This type can be used in AddComponent for example.

Keep in mind that MonoScript is an editor class, so don’t use it in runtime classes.