'My data resets when I apply the change in a custom inspector

I'm doing a Visual Scripting For unity, but I when I set a value to a value using my custom editor it just make it default, here the code of the I know for the tag UnityEditor.CustomEditor, But I have it on my script so basicly the custom editor works but it just the variables that just reset

custom editor:

public class CustomEditor : Editor
{
    public SerializedProperty
         state_Prop,
         pos_Prop,
         text_Prop,
         number_Prop;
    private void OnEnable()
    {
        state_Prop = serializedObject.FindProperty("var_type");
        pos_Prop = serializedObject.FindProperty("pos");
        text_Prop = serializedObject.FindProperty("text");
        number_Prop = serializedObject.FindProperty("number");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        SetVariables t = target as SetVariables;

        EditorGUILayout.PropertyField(state_Prop);

        SetVariables.type_of_variable st = (SetVariables.type_of_variable)state_Prop.enumValueIndex;
        
        EditorGUILayout.ObjectField(serializedObject.FindProperty("variable"));

        switch (st)
        {
            case SetVariables.type_of_variable.POSITION:
                
                EditorGUILayout.Vector3Field("Position", t.pos);
                break;
            case SetVariables.type_of_variable.TEXT:
                EditorGUILayout.TextField("Text", t.text);
                break;
            case SetVariables.type_of_variable.NUMBER:
                EditorGUILayout.FloatField("Number", t.number);
                break;
        }
        

        serializedObject.ApplyModifiedProperties();
    }
}


Solution 1:[1]

First of all you would need to assign the returned values to something

t.pos = EditorGUILayout.Vector3Field("Position", t.pos);

and

t.text = EditorGUILayout.TextField("Text", t.text);

and

t.number = EditorGUILayout.FloatField("Number", t.number);

and also actually

serializedObject.FindProperty("variable").objectReferenceValue = EditorGUILayout.ObjectField(serializedObject.FindProperty("variable"));

BUT DO NOT DO THIS!


In general do not assign things directly to the target (unless you know exactly what you are doing)!

This lacks dirty state handling and therefore there will be no Undo/Redo and also no persistent saving! It might appear to be assigned in the Inspector but you might be losing those changes with the next reload (e.g. scene switch, re-import or simply closing and re-opening the project)!

You either have to get the values, assign them and explicitly mark your object dirty e.g. using Undo.RecordObject like

switch (st)
{
    case SetVariables.type_of_variable.POSITION:
        using (var check = new EditorGUI.ChangeCheckScope())
        {
            var newPos = EditorGUILayout.Vector3Field("Position", t.pos);

            if (check.changed)
            {
                Undo.RecordObject(target, "Changed position");
                t.pos = newPos;
            }
        }
        break;

    case SetVariables.type_of_variable.TEXT:
        using (var check = new EditorGUI.ChangeCheckScope())
        {
            var newText = EditorGUILayout.TextField("Text", t.text);

            if (check.changed)
            {
                Undo.RecordObject(target, "Changed text");
                t.text = newText;
            }
        }      
        break;

    case SetVariables.type_of_variable.NUMBER:
        using (var check = new EditorGUI.ChangeCheckScope())
        {
            var newNumber = EditorGUILayout.FloatField("Number", t.number);

            if (check.changed)
            {
                Undo.RecordObject(target, "Changed number");
                t.number = newNumber;
            }
        }     
        break;
}

Or - and I would prefer that - why not again go through proper SerializedProperties you already have anyway and rather let Unity decide how to draw them like

switch (st)
{
    case SetVariables.type_of_variable.POSITION:
        // This would simply use the name according to your field name ("Pos" in your case)
        //EditorGUILayout.PropretyField(pos_Prop);
        // This rather overwrites with a custom name
        EditorGUILayout.PropretyField(pos_Prop, new GUIContent("Position"));
        break;

    case SetVariables.type_of_variable.TEXT:
        EditorGUILayout.PropretyField(text_Prop);
        break;

    case SetVariables.type_of_variable.NUMBER:
        EditorGUILayout.PropertyField(number_Prop);
        break;
}

and also

// Just as an example without the label
EditorGUILayout.PropertyField(serializedObject.FindProperty("variable"), GUIContent.None);

The EditorGUILayout.PropertyField already deals with all the dirty states and Undo/Redo handling for you as long as you call the

serializedObject.ApplyModifiedProperties();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 derHugo