Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Custom Drawn variables reset on start

Discussion in 'Scripting' started by puddleglum, Jul 14, 2023.

  1. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    380
    for some reason my custom drawn variables, get reset on start and have no idea what's causing this.
    im very new to this syntax and i followed a tutorial to get this far. so it might be something obvious im just oblivious to.

    here is my code

    Code (csharp):
    1.  
    2.  
    3.    [HideInInspector]
    4.     public bool AutoLockM;
    5.     public GameObject AttackM;
    6.     public Vector2 OffsetM;
    7. #if UNITY_EDITOR
    8. [CustomEditor(typeof(Attacks))]
    9.     public class AttackCustomEditor : Editor
    10.     {
    11.         public override void OnInspectorGUI()
    12.         {
    13.         base.OnInspectorGUI();
    14.  
    15.         Attacks AttacksCast = (Attacks)target;
    16.  
    17.  
    18.             DrawMelee(AttacksCast);
    19.         }
    20. }
    21.  
    Code (csharp):
    1.  
    2.     static void DrawMelee(Attacks AttacksCast){
    3.  
    4.         EditorGUILayout.Space(20);
    5.  
    6.         EditorGUILayout.LabelField("Melee Settings", EditorStyles.boldLabel);
    7.         EditorGUILayout.Space();
    8.         EditorGUILayout.BeginHorizontal();
    9.  
    10.         EditorGUILayout.LabelField("Lock To Enemy", GUILayout.MaxWidth(145));
    11.         AttacksCast.AutoLockM = EditorGUILayout.Toggle(AttacksCast.AutoLockM);
    12.         EditorGUILayout.EndHorizontal();
    13.  
    14.         EditorGUILayout.BeginHorizontal();
    15.  
    16.         EditorGUILayout.LabelField("Attack Prefab", GUILayout.MaxWidth(145));
    17.    
    18.             GameObject x = EditorGUILayout.ObjectField(AttacksCast.AttackM, typeof(GameObject), true) as GameObject;
    19.         AttacksCast.AttackM = x;
    20.         EditorGUILayout.EndHorizontal();
    21.         EditorGUILayout.BeginHorizontal();
    22.  
    23.        // EditorGUILayout.LabelField("Offset", GUILayout.MaxWidth(145));
    24.         AttacksCast.OffsetM = EditorGUILayout.Vector2Field("Offset", AttacksCast.OffsetM);
    25.         EditorGUILayout.EndHorizontal();
    26.     }
    27. #endif
    28.  
    here is what it looks like pre and post play
    pre: upload_2023-7-14_2-53-6.png

    Post: upload_2023-7-14_2-53-26.png
    the variables its defaulting to, is for some reason a previous state. idk
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Whenever you change values via code you need to inform Unity these values have changed. In your case you never inform Unity the object has been changed (known as 'dirty'), so the values aren't written to disk.

    Generally in IMGUI land you use
    EditorGUI.Begin/EndChangeCheck()
    to check if values have been changed, and record the object with the
    Undo
    class, or use
    EditorUtility.SetDirty
    .

    Or you can use the SerialisedProperty system to manages this for you more automatically: https://docs.unity3d.com/ScriptReference/SerializedProperty.html
     
  3. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    380
    ooooh. alr thx, that makes sense.