Get float from class in a SerializedProperty

I am trying to start using sciptable objects as describe in this talk. Here is my code:

// FloatVariable Class

using UnityEngine;

[CreateAssetMenu]
public class FloatVariable : ScriptableObject
{
    public float value;
}
// FloatReference

using UnityEngine;
using System;

[Serializable]
public class FloatReference
{
    public bool use_constant = true;
    public float constant_value;
    public FloatVariable variable_value;

    public float v
    {
        get
        {
            return use_constant ? constant_value : variable_value.value;
        }
        set
        {
            if (use_constant) throw new Exception("Cannot assign constant_value");
            else variable_value.value = value;
        }
    }
}
// GameplayManager

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;
using System;


public class GameplayManager : MonoBehaviour
{
    public FloatReference pl_orb_mode;
}
// GameplayManagerEditor

using UnityEngine;
using UnityEditor;
using System;

[CustomEditor(typeof(GameplayManager))]
public class GameplayManagerEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        GameplayManager gameplay_mag = (GameplayManager)serializedObject.targetObject;

        SerializedProperty pl_orb_mode = serializedObject.FindProperty("pl_orb_mode");
        SerializedProperty variable = pl_orb_mode.FindPropertyRelative("variable_value");
        SerializedProperty the_value = variable.FindPropertyRelative("value");

        float test = the_value.floatValue;
        Debug.Log(test);
    }

}

When I try to get my float test = the_value.floatValue; I get an error:

NullReferenceException: Object reference not set to an instance of an object
GameplayManagerEditor.OnInspectorGUI () (at Assets/Shared/Scripts/Editor/GameplayManagerEditor.cs:18)

So I can get the FloatVariable variableclass as a SerializedProperty but I can’t get its float value property. Why is that so and how to make it work?

Did you assign your variable_value? It’s a ScriptableObject, ie. an asset. So it has to be created and assigned before you can set it’s values.

1 Like

I got an answer here. Hope it will help someone one day :slight_smile:

Yes it is defined. And I found an answer here. Thanks!