Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Custom Inspector deletes all variables after the starting game

Discussion in 'Scripting' started by flo16022003, Jan 25, 2021.

  1. flo16022003

    flo16022003

    Joined:
    Oct 15, 2020
    Posts:
    2
    I have a custom Inspector with a list of Gameobjects. And everytime I start the game or switch scenes everything in the custom inspector gets deleted.

    Before starting the Game it looks like this:
    upload_2021-1-25_16-28-59.png

    After starting the Game everything is cleared:
    upload_2021-1-25_16-38-0.png

    I attached the Enemy.cs file, please help me.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. [System.Serializable]
    8. public class Enemy : MonoBehaviour {
    9.  
    10.     public float speed = 3.0f;
    11.     public float viewDistance = 5.0f;
    12.  
    13.     public bool followPath;
    14.  
    15.     [HideInInspector]
    16.     private MyList pathPoints;
    17.  
    18.     public MyList PathPoints {
    19.         get {
    20.             if (pathPoints == null) {
    21.                 pathPoints = ScriptableObject.CreateInstance<MyList>();
    22.             }
    23.             return pathPoints;
    24.         }
    25.         set {
    26.             pathPoints = value;
    27.         }
    28.     }
    29.     Transform target;
    30.     Rigidbody2D rigidbody2d;
    31.     NavMeshAgent agent;
    32.  
    33.     void Start(){
    34.         target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    35.         rigidbody2d = GetComponent<Rigidbody2D>();
    36.         agent = GetComponent<NavMeshAgent>();
    37.         agent.updateRotation = false;
    38.         agent.updateUpAxis = false;
    39.         agent.speed = speed;
    40.     }
    41.  
    42.     void Update(){
    43.  
    44.         if(Vector2.Distance(transform.position, target.transform.position) < viewDistance) {
    45.             agent.SetDestination(target.position);
    46.             agent.isStopped = false;
    47.         } else {
    48.             agent.isStopped = true;
    49.         }
    50.     }
    51. }
    52.  
    53. [CustomEditor(typeof(Enemy))]
    54. public class EnemyEditor : Editor {
    55.     Enemy enemy;
    56.     SerializedObject serializedMyList;
    57.  
    58.     void Awake() {
    59.         enemy = target as Enemy;
    60.  
    61.         serializedMyList = new UnityEditor.SerializedObject(enemy.PathPoints);
    62.     }
    63.  
    64.     public override void OnInspectorGUI() {
    65.         base.OnInspectorGUI();
    66.  
    67.         if (enemy.followPath) {
    68.             serializedMyList.Update();
    69.  
    70.             SerializedProperty serializedPropertyList = serializedMyList.FindProperty("list");
    71.  
    72.             EditorGUILayout.PropertyField(serializedPropertyList, new GUIContent("Path Points"), true);
    73.  
    74.             serializedMyList.ApplyModifiedProperties();
    75.         }
    76.  
    77.         if (GUI.changed) {
    78.             Undo.RecordObject(target, "Enemy");
    79.             EditorUtility.SetDirty(target);
    80.         }
    81.     }
    82. }
    83.  
    84. [System.Serializable]
    85. public class MyList : ScriptableObject {
    86.     public List<GameObject> list;
    87. }
    88.  
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    I think you need to hit that Undo.RecordObject BEFORE you change the serializable data in the target, not after.
     
  3. flo16022003

    flo16022003

    Joined:
    Oct 15, 2020
    Posts:
    2
    I tried it to put it at the start, but still the same Problem.

    Code (CSharp):
    1. public override void OnInspectorGUI() {
    2.         base.OnInspectorGUI();
    3.  
    4.         if (enemy.followPath) {
    5.             Undo.RecordObject(target, "Enemy");
    6.  
    7.             serializedMyList.Update();
    8.  
    9.             SerializedProperty serializedPropertyList = serializedMyList.FindProperty("list");
    10.  
    11.             EditorGUILayout.PropertyField(serializedPropertyList, new GUIContent("Path Points"), true);
    12.  
    13.             serializedMyList.ApplyModifiedProperties();
    14.         }
    15.  
    16.         if (GUI.changed) {
    17.             EditorUtility.SetDirty(target);
    18.         }
    19.     }
     
  4. schakalskopf

    schakalskopf

    Joined:
    Sep 15, 2018
    Posts:
    4
    make private MyList pathPoints; -> public
    that should be the problem
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,919
    While you are right that the variable pathPoints is not serialized and that's the issue, you shouldn't make it public when it doesn't need to be public. You should add the SerializedField attribute:

    Code (CSharp):
    1. [SerializeField, HideInInspector]
    2. private MyList pathPoints;