title: Unity Dynamic Serializable Field
body: <p>Is there a way to set a dynamic serializable field within unity? I have tried using <code>object</code> and <code>UnityEngine.Object</code>, both give errors.</p>
  
  <p><code>object</code> doesn't seem to be serializable, because <code>value</code> is null.</p>
  
  <blockquote>
    <p>NullReferenceException: Object reference not set to an instance of an object</p>
  </blockquote>
  
  <p><code>UnityEngine.Object</code> is serializable, but when I try to set an int to it I get the following error:</p>
  
  <blockquote>
    <p>type is not a supported int value</p>
  </blockquote>
  
  <p>So what is a way that I can set a dynamic type to the variable?</p>
  
  <pre><code>public enum ValueType { Integer, Float, String, Boolean, GameObject }
  
  public class SetVariable : ScriptableObject {
    public ValueType valueType;
    public string key;
    public object value;
  }
  </code></pre>
  
  <p>I then have a custom inspector script that looks like this:</p>
  
  <pre><code>SerializedObject serializedObject;
  
  public void SetObject(MyObject obj) {
    serializedObject = new SerializedObject(obj);
  }
  
  public override void OnDisplay() {
    accessType = serializedObject.FindProperty("accessType");
    key = serializedObject.FindProperty("key");
    value = serializedObject.FindProperty("value");
  }
  
  public override void OnGUI() {
    EditorGUILayout.PropertyField(valueType);
    EditorGUILayout.PropertyField(key);
  
    if (key.stringValue.Trim().Length &gt; 0) {
      switch (valueType.enumNames[valueType.enumValueIndex]) {
        case "Integer":
          value.intValue = EditorGUILayout.IntField(value.displayName, value.intValue);
          break;
      }
    }
  }
  </code></pre>