How to set initial values ββfor custom inspector fields in unity
I have two similar classes ClassA
and ClassB
. Both classes contain bool:
In class A:
[SerializeField]
private bool _overwrite = true;
public bool overwrite
{
get { return _overwrite; }
set { _overwrite = value; }
}
In class B:
[SerializeField]
private bool _hide = true;
public bool hide
{
get { return _hide; }
set { _hide = value; }
}
Both scripts have CustomEditor
script. In both editor scripts, within the method OnInspectorGUI()
, the following two lines are used to add the appropriate bool to the inspector.
ClassA.overwrite = EditorGUILayout.ToggleLeft("Overwrite", ClassA.overwrite);
ClassB.hide = EditorGUILayout.ToggleLeft("Hide", ClassB.hide);
When I add ClassA to GameObject the "Overwrite" field is unchecked, however when I add ClassB to the GameObject the "Hide" field is checked.
I don't understand what is different, or what other factor is involved in setting the default / initial value for a property.
Ideally I want both of them checked by default.
Any ideas what I might be missing?
Thank you for your time,
Liam
source to share
What is it grabObject
?
If it's a component variable where you found a reference in the method OnEnable
, use the target
Editor
script variable and then just change the editor scripts to:
grabObject.overwrite = EditorGUILayout.ToggleLeft("Overwrite", grabObject.overwrite);
and
grabObject.hide = EditorGUILayout.ToggleLeft("Hide", grabObject.hide);
should solve your problem.
source to share