Hide null scripts in the inspector

I’m working on a game and I designed it so each character is given a script. The player holds items which also have scripts. It works something like this

public class MyPlayer {

int health;
int mana;

ItemInfo weapon;
ItemInfo shield;


ItemInfo is a script I made that has simple infomation in it. I use it for all my items. The problem is the character isn’t always holding a weapon or shield, so I check to see if weapon == null. Normally this always works, but if I view MyPlayer in the inspector then it automatically creates a blank scripts for ItemInfo, making them not null… I could hide them, but I would like to be able to see them in they aren’t null, for testing purposes.

As for my question, is there a way to hide null scripts in the inspector?

There is and it’s custom inspector. Edit your script like this:

public class MyPlayer  : MonoBehaviour{
	public int health;
	public int mana;
	[SerializeField] public ItemInfo weapon;
	[SerializeField] public ItemInfo shield;

And put this class in a folder named Editor:

public class MyPlayerEditor : Editor {

	SerializedProperty weapon;
	SerializedProperty shield;
	private void OnEnable() {
		weapon = serializedObject.FindProperty("weapon");
		shield = serializedObject.FindProperty("shield");
	public override void OnInspectorGUI() {
		var myPlayer = target as MyPlayer;
		myPlayer.health = EditorGUILayout.IntField("Health", myPlayer.health);
		myPlayer.mana = EditorGUILayout.IntField("Mana", myPlayer.mana);
		//Show this item in the inspector only if it's not null 
		if(myPlayer.weapon != null){
			EditorGUILayout.PropertyField(weapon, GUIContent.none);
		if(myPlayer.shield != null){
			EditorGUILayout.PropertyField(shield, GUIContent.none);

Wow thanks, but there’s a problem. It’s seems that even if I hide the null script unity still makes a instants of it if I look at the class it’s attached to.

That’s not possible for custom serializable classes if you want them to be serialized and therefore visible in the inspector. The serialization system treats custom serializable classes like structs. So the inspector will always automatically create an instance of those classes. That’s also the reason why self and circular references are not supported.

Make sure you read the documentation about script serialization carefully. Especially the part about custom serializable classes which starts at the line:

When might the serializer behave unexpectedly?

If you actually want references to classes, those classes need to be derived either from MonoBehaviour (and attached to a gameobject) or from ScriptableObject. In this case they are seperate objects and the field in the inspector doesn’t show the class fields inline but just a reference to the referenced object.