Use "object" reference type in Scriptable Object

Hi, I am currently working on A form of Developer Console system (similar to the ones you’d see in Source Engine games) & I am currently trying to get the scriptable object to reference an object type, which will change in preview type depending on what the enum value is set to. So far, the closest I got to making it work was through SerializedProperty.managedReferenceValue. It does everything I want it to, except for saving the data itself. I don’t know why, but does anyone know how to resolve this issue? Or any other alternatives that give the intended output? I’ve tried searching for the answer online, but it appears nobody has asked this question before.

Heres A stripped down version of the script for you to see & try for yourself

using UnityEditor;
using UnityEngine;

[CreateAssetMenu(fileName = "convar", menuName = "Dev Console/ConVar", order = 0)]
public class ConVar : ScriptableObject
{
    public enum ConVarType
    {
        Bool = 0,
        Int,
        Float,
        String,
    }

    public ConVarType type = ConVarType.String;
    [SerializeReference] public object value = "";
}

[CustomEditor(typeof(ConVar))]
public class ConVar_Inspector : Editor
{
    public override void OnInspectorGUI()
    {
        SerializedProperty type = serializedObject.FindProperty("type");
        SerializedProperty value = serializedObject.FindProperty("value");

        EditorGUILayout.LabelField(new GUIContent("Value"), EditorStyles.boldLabel);
        type.enumValueIndex = EditorGUILayout.Popup(new GUIContent("Value Type"), type.enumValueIndex, new GUIContent[] { new GUIContent("Bool"), new GUIContent("Int"), new GUIContent("Float"), new GUIContent("String") });
        switch (type.enumValueIndex)
        {
            case 0: value.managedReferenceValue = EditorGUILayout.Toggle(new GUIContent("Value"), ConversionTools.ToBool(value.managedReferenceValue)); break;
            case 1: value.managedReferenceValue = EditorGUILayout.IntField(new GUIContent("Value"), ConversionTools.ToInt(value.managedReferenceValue)); break;
            case 2: value.managedReferenceValue = EditorGUILayout.FloatField(new GUIContent("Value"), ConversionTools.ToFloat(value.managedReferenceValue)); break;
            case 3: value.managedReferenceValue = EditorGUILayout.TextField(new GUIContent("Value"), ConversionTools.ToString(value.managedReferenceValue)); break;
        }

        serializedObject.ApplyModifiedProperties();
    }
}

oh, and heres the ConversionTools script since your probably gonna get errors on that
ConversionTools.cs (4.0 KB)

From the docs from SerializeReference: Unity - Scripting API: SerializeReference

Must not be a C# Value type. Therefore simple types like integers, as well as structures are not supported, and should be serialized without the [SerializeReference] attribute instead.

2 Likes

So, you mean just simply remove the [SerializeReference] tag or just replace it with something like [SerializeField] instead?

No, what I mean is that what you’re trying doing is unsupported by Unity’s serialisation system.

1 Like

Ah, I see. In that case, i’ll try to find A workaround for it or just split the object variable into the 4 types i’m after…

Thanks for trying to help.