- Home /
How to make [HideInInspector] show in Debug Mode?
Hey. Many times when I want to use a public variable so that other scripts can access it, but I don't want sometimes that specific variable to show up in the Inspector. That's why there is [HideInInspector]. However, [HideInInspector] also hides the variable in Debug Mode, making it really hard and annoying to debug. Does anyone have a workaround around this?
Answer by Hellium · May 22, 2021 at 07:48 PM
That solution is a terrible one. You multiply the number of variables, so you may modify the wrong one in code, and you use an Update just for assigning a variable which can lead to errors if you modify myPrivateVar, and the myPublicVar does not update "in time" before being read by another script.
There are multiple solutions to your problem.
Using properties
public class MyClass : MonoBehaviour {
private float myPrivateVar = 5f;
public float myPublicVar { get => myPrivateVar ; set => myPrivateVar = value; }
}
Using an attribute
HideInNormalInspector.cs
public class HideInNormalInspector : UnityEngine.PropertyAttribute
{
public HideInNormalInspector()
{
}
}
HideInNormalInspectorDrawer .cs in an Editor folder
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(HideInNormalInspector))]
public class HideInNormalInspectorDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return -2; // To compensate the gap between inspector's properties
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
}
}
public class MyClass : MonoBehaviour {
[HideInNormalInspector]
public float myPublicVar;
}
I see thank you. I'll accept your answer now and delete $$anonymous$$e.
Your answer