How to remove "Script" field in inspector?

There is a “Script” field in the inspector that allows quick replacement of a MonoBehaviour, but I don’t want it to show…

3403-immagine.png

Is there a way to hide it without having to rewrite OnInspectorGUI completely.
Thanks!

Hi,

I know this is kind of old now, but I saw that people were still responding.

This is actually very simple to achieve, but it will involve a custom editor of sorts.

The property you’re looking to exclude is “m_Script”, as others have said and Unity actually gives you a nice alternative to DrawDefaultInspector that allows excluding properties: DrawPropertiesExcluding. They’re not exactly equivalent (the names should hint at what they’re both doing), but it makes it very easy to get your desired behaviour. DrawPropertiesExcluding has the following signature:

protected internal static void DrawPropertiesExcluding(SerializedObject obj, params string[] propertyToExclude);

You can actually use this to exclude any properties you don’t want showing up.

So, simply call it from within an editor script, passing the SerializedObject (in this case, it would simply be the serializedObject that inheriting from Editor gives you access to).

Make sure you call serializedObject.Update() before, and serializedObject.ApplyModifiedProperties() after.

So, a basic implementation could be like so:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(MyClassIDontWantToSeeScriptIn))]
public class ScriptlessEditor : Editor
{
    private static readonly string[] _dontIncludeMe = new string[]{"m_Script"};
    
    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        DrawPropertiesExcluding(serializedObject, _dontIncludeMe);

        serializedObject.ApplyModifiedProperties();
    }
}

Now, that still seems like a bit to add when you want something as simple as removing a property, so I recommend creating something you can inherit from. If you only want to remove the script property then something like this would do you:

ScriptlessEditor.cs:

    using UnityEngine;
    using UnityEditor;
    
    public abstract class ScriptlessEditor : Editor
    {
        private static readonly string[] _dontIncludeMe = new string[]{"m_Script"};
        
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
    
            DrawPropertiesExcluding(serializedObject, _dontIncludeMe);
    
            serializedObject.ApplyModifiedProperties();
        }
    }

And then any MonoBehaviour you wanted to act like this, you’d just create:

MyScriptlessMonoBehaviourInspector.cs:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(MyScriptlessMonoBehaviour))]
public class MyScriptlessMonoBehaviourInspector : ScriptlessEditor
{}

Personally, I find I regularly want to make minor tweaks to how an Inspector displays, but not enough to justify reimplementing everything, so I actually use a variant of the following script to derive from, which makes minor tweaks/small custom behaviour a breeze. It should speak for itself, but let me know if you want me to explain any of it.

TweakableEditor.cs:

using UnityEngine;
using System.Collections;
using UnityEditor;

/// <summary>
/// A simple class to inherit from when only minor tweaks to a component's inspector are required.
/// In such cases, a full custom inspector is normally overkill but, by inheriting from this class, custom tweaks become trivial.
/// 
/// To hide items from being drawn, simply override GetInvisibleInDefaultInspector, returning a string[] of fields to hide.
/// To draw/add extra GUI code/anything else you want before the default inspector is drawn, override OnBeforeDefaultInspector.
/// Similarly, override OnAfterDefaultInspector to draw GUI elements after the default inspector is drawn.
/// </summary>
public abstract class TweakableEditor : Editor
{
    private static readonly _emptyStringArray = new string[0];
    
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        
        OnBeforeDefaultInspector();
        DrawPropertiesExcluding(serializedObject, GetInvisibleInDefaultInspector());
        OnAfterDefaultInspector();

        serializedObject.ApplyModifiedProperties();
    }

    protected virtual void OnBeforeDefaultInspector()
    {}

    protected virtual void OnAfterDefaultInspector()
    {}

    protected virtual string[] GetInvisibleInDefaultInspector()
    {
        return _emptyStringArray;
    }
}

Then simply inherit and override any features you want to use, for example:

ExampleEditor.cs:

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(Example))]
public class ExampleEditor : TweakableEditor
{
    // Stops showing the script field
    protected override string[] GetInvisibleInDefaultInspector()
    {
        return new[] { "m_Script" };
    }

    // Just showing other stuff we can add
    protected override void OnBeforeDefaultInspector()
    {
        GUILayout.Label("What a fancy Inspector this is!");
    }
}

I’ve found this approach a nice balance between having some control, but being able to mostly default to the default inspector (except when I really need to make something custom because that’s what’s appropriate for the script).

You just have to implement a custom inspector for your class(es). You have to scroll half way down since the first part is about EditorWindows.

btw there’s no way to not “rewrite” the GUI since the DefaultInspector works as it is.

I have come up with something to put into your Editor folder that will hide that field in everything using a default inspector and provide a method to replace DrawDefaultInspector in your code to do the same.

(Unfortunately, since extension methods cannot override/hide existing methods, any code that calls Editor.DrawDefaultInspector that you cannot change will still show the Script field.)

(For reference, the field in question is “m_Script” in the SerializedObject and seems to always come first.)

using UnityEngine ;
using UnityEditor ;

[ CustomEditor ( typeof ( MonoBehaviour ) , true ) ]
public class DefaultInspector : Editor
{
	public override void OnInspectorGUI ( )
	{
		this . DrawDefaultInspectorWithoutScriptField ( ) ;
	}
}

public static class DefaultInspector_EditorExtension
{
	public static bool DrawDefaultInspectorWithoutScriptField ( this Editor Inspector )
	{
		EditorGUI . BeginChangeCheck ( ) ;
		
		Inspector . serializedObject . Update ( ) ;
		
		SerializedProperty Iterator = Inspector . serializedObject . GetIterator ( ) ;
		
		Iterator . NextVisible ( true ) ;
		
		while ( Iterator . NextVisible ( false ) )
		{
			EditorGUILayout . PropertyField ( Iterator , true ) ;
		}
		
		Inspector . serializedObject . ApplyModifiedProperties ( ) ;
		
		return ( EditorGUI . EndChangeCheck ( ) ) ;
	}
}