Search Unity

Bug Color field is always resetting on inspector

Discussion in 'Editor & General Support' started by abarcacrespom, Feb 15, 2023.

  1. abarcacrespom

    abarcacrespom

    Joined:
    Feb 1, 2020
    Posts:
    10
    Im overriding the button component, so im isung a custom editor to view the public components on the incpector. It works well, but after 4 color fields, the 5th its not working, you can select the color but when you exit prefab mode or entero on play mode, it resets to black.
    Code (CSharp):
    1. public override void OnInspectorGUI()
    2.     {
    3.         DynamicButton targetButton = (DynamicButton)target;
    4.         targetButton.Background = (Image) EditorGUILayout.ObjectField("Background Image", targetButton.Background, typeof(Image), true);
    5.         targetButton.NormalBGColor = EditorGUILayout.ColorField("\tBackground Color", targetButton.NormalBGColor);
    6.         targetButton.DisabledBGColor = EditorGUILayout.ColorField("\tDisabled Color", targetButton.DisabledBGColor);
    7.         targetButton.Text = (TextMeshProUGUI) EditorGUILayout.ObjectField("Text Component", targetButton.Text, typeof(TextMeshProUGUI), true);
    8.         targetButton.NormalTextColor = EditorGUILayout.ColorField("\tText Normal Color", targetButton.NormalTextColor);
    9.         targetButton.PressedTxtColor =EditorGUILayout.ColorField("\tText Pressed Color", targetButton.PressedTxtColor);
    10.         targetButton.DisabledTextColor =EditorGUILayout.ColorField("\tText Disabled Color", targetButton.DisabledTextColor);
    11.         targetButton.testColor =EditorGUILayout.ColorField("\ttestColor", targetButton.testColor);
    12.         base.OnInspectorGUI();
    13.     }
    All the Color fields works well, just the pressedTxtColor doesnt work.
    Code (CSharp):
    1. private const float BG_FILL_OUT = 0.2f;
    2.     public Image Background;
    3.     public Color NormalBGColor;
    4.     public Color DisabledBGColor;
    5.     public TextMeshProUGUI Text;
    6.     public Color NormalTextColor;
    7.     public Color PressedTxtColor;
    8.     public Color DisabledTextColor;
    These are the fields on the main component, as you can see there isnt any anormality.


    Code (CSharp):
    1. Text.color = PressedTxtColor;
    This is the only usage of the PressedTxtColor field, it isnt called or used on any other place.

    My unity version is 2021.3.5.f1, i cant see any difference between PressedTxtColor and the other color fileds, so i think is a bug. Does somebody know why this could be happening? (maybe a memory leak or something like that)
     
    Last edited: Feb 15, 2023
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Please use Code-tags instead of screenshots.

    1. In your first script, you're not using SerializedProperties. This means that there's a possibility that your changes are not written to the object.
    2. All the yellow lines in the 2nd script are likely because you need to use "new" since you're overriding non-virtual fields?

    "Does somebody know why this could be happening?"
    Probably the lack of usage of SerializedProperty. The value gets overridden in base.OnInspectorGUI().
     
  3. abarcacrespom

    abarcacrespom

    Joined:
    Feb 1, 2020
    Posts:
    10
    The yellow lines are because they dont match the suggested name rules, they are no relevant.
    Using SerializedField doesnt affect the color, after pressing play it returns to black.

    After messing around i found that it only happens if a prefab of the object is created. Unpacking the prefab, changing it in the inspector and creating again the prefab does the work, but it would be a pain to do with a lof of prefabs instantiated. So i dont know how to solve it.
     
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    SerializedProperty, not SerializedField.
    Unity - Manual: Custom Editors (unity3d.com)

    DynamicButton targetButton = (DynamicButton)target;

    This line should not be part of your Custom Inspector, unless in specific edgecases.
    In 99% of cases you should be using
    serializedObject.FindProperty()
     
  5. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Code (CSharp):
    1. public override void OnInspectorGUI()
    2. {
    3.     serializedObject.Update(); // Update Serialized-Values on Object (ensure that values are up-to-date)
    4.     EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(DynamicButton.Background)));
    5.     EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(DynamicButton.NormalBGColor)));
    6.     EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(DynamicButton.DisabledBGColor)));
    7.     EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(DynamicButton.Text)));
    8.     EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(DynamicButton.NormalTextColor)));
    9.     EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(DynamicButton.PressedTxtColor)));
    10.     EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(DynamicButton.DisabledTextColor)));
    11.     EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(DynamicButton.testColor)));
    12.     serializedObject.ApplyModifiedProperties(); // Apply changes made between serializedObject.Update() and this line to the Object
    13.     base.OnInspectorGUI();
    14. }
     
    abarcacrespom likes this.
  6. abarcacrespom

    abarcacrespom

    Joined:
    Feb 1, 2020
    Posts:
    10
    It worked like a charm, TY