How to work with custom class (ScriptableObject) SerializedProperty?

Hello, im trying to make customclasses that are derived from ScriptableObject work with SerializedProperty

Below, i have made a very minimal and simple example that works without deriving from ScriptableObject.


public class Human : MonoBehaviour
	public Weapon Weapon;
	public int Health;


public class Weapon
	public int Damage;
	public int Weight;


public class HumanEditor : Editor
	SerializedObject sObj;

	void OnEnable()
		this.sObj = new SerializedObject(target);

		Weapon weapon = ((Human)target).Weapon;
		if (weapon == null)
			weapon = new Weapon();
	public override void OnInspectorGUI()

		SerializedProperty sHealth = sObj.FindProperty("Health");
		sHealth.intValue = EditorGUILayout.IntField("Health", sHealth.intValue);

		SerializedProperty sWeaponDamage = sObj.FindProperty("Weapon.Damage");
		sWeaponDamage.intValue = EditorGUILayout.IntField("Damage", sWeaponDamage.intValue);

		SerializedProperty sWeaponWeight = sObj.FindProperty("Weapon.Weight");
		sWeaponWeight.intValue = EditorGUILayout.IntField("Weight", sWeaponWeight.intValue);


This works. But when i derived weapon from ScriptableObject, and change the OnEnable() function to:

	void OnEnable()
		this.sObj = new SerializedObject(target);
		SerializedProperty sWeapon = sObj.FindProperty("Weapon");

		if (sWeapon.objectReferenceValue == null)
			sWeapon.objectReferenceValue = ScriptableObject.CreateInstance<Weapon>();

Then i get an exepction saying its null.
What should i do to make it work with ScriptableObject?
The reason i need this, is because i want to use polymorphism and inheritance with Weapon.

ScriptableObjects need to have an asset representation. So, you need to save the instance somewhere (e.g. save as subasset):

using UnityEngine;
using UnityEditor;

public class HumanEditor : Editor {
	void OnEnable () {

		serializedObject.Update ();

		SerializedProperty serializedWeapon = serializedObject.FindProperty ("Weapon");
		if (serializedWeapon.objectReferenceValue == null) {
			Weapon w = ScriptableObject.CreateInstance<Weapon> ();
                    // You can configure w.hideFlags here wisely
			AssetDatabase.AddObjectToAsset (w, target);
			serializedWeapon.objectReferenceValue = w;

		serializedObject.ApplyModifiedProperties ();

	public override void OnInspectorGUI () {

		serializedObject.Update ();

		SerializedProperty health = serializedObject.FindProperty ("Health");
		health.intValue = EditorGUILayout.IntField ("Health", health.intValue);

		SerializedObject weaponSerializedObject = new SerializedObject (serializedObject.FindProperty ("Weapon").objectReferenceValue);

		weaponSerializedObject.Update ();

		SerializedProperty weaponDamage = weaponSerializedObject.FindProperty ("Damage");
		weaponDamage.intValue = EditorGUILayout.IntField ("Damage", weaponDamage.intValue);
		SerializedProperty weaponWeight = weaponSerializedObject.FindProperty ("Weight");
		weaponWeight.intValue = EditorGUILayout.IntField ("Weight", weaponWeight.intValue);

		weaponSerializedObject.ApplyModifiedProperties ();


About inheritance and polymorphism, you should look at this.