- Home /
CustomInspector display a private variable
I'm looking for a quick way to display a label with the value of a private variable from my script in the inspector. The inspector debug mode does this already, but I basically want to replicate that functionality to display labels for private variables in the regular inspector.
Can this be achieved with some Attribute or access modifier or do I need to find it via Reflection? serializedObject.FindProperty returns null, when I try it like this:
public class HealthAttribute : BaseAttribute
{
[SerializeField]
private float m_MaxHealth = 100f;
private float m_Health;
}
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(HealthAttribute))]
public class HealthAttributeInspector : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Health"));
}
}
#endif
When I mark m_Health with [SerializeField] it works, of course, but I don't want to serialize the value, it's runtime only.
Answer by Geometrical · Feb 22, 2017 at 10:04 PM
If you exposed the private variable with a property that also allows the setting of data, then it's quite easy.
In your script: float f;
pub float floatProperty { get { return this.f; } set { this.f = value; } }
In the custom editor script:
yourObject.floatProperty = EditorGUILayout.FloatField("floatValue", yourObject.floatProperty...);