- Home /
Private list is serialized inside a prefab
Hello,
I have a prefab with a protected list. The list is serialized inside the prefab, even when I stop the editor.
I have no clue how this is happening. The problem is when I play the editor, the list keeps its last state, instead of be a new one as it should be.
Of course I just could clean the list, but this is a very weird behaviour and I would like to understand what is really happening.
I have an intuition that is about that the prefab has DontDestroyOnLoad, but I have tried to reproduce the same problem with a very small script without success.
Any idea about this could be happening?
Thank you
Hi, I think it's because Unity uses some kind of resource manager to manage it's prefabs that optimizes loading/deserializing/object life. The prefab is then never removed from memory until the editor is closed (think of it as a memory-mapped file). This is very convenient to modify a prefab with editor code and keep the modifications on the disc (e.g. procedural/generated data), but it can be disturbing with private fields. The general best practice is that a prefab should only contain read-only data or be instantiated. Usually, it ends up with 2 objects, 1 containing readonly data and one containing dynamic data during gameplay. It can then be convenient & more optimized to use ScriptableObjects ins$$anonymous$$d of gameObjects + $$anonymous$$onobehaviors to contain readonly static data.
Your answer
![](https://koobas.hobune.stream/wayback/20220612084853im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How do I make child classes in a list editable from the Inspector? 1 Answer
What is runtime analogy for Editor's PrefabUtility.SetPropertyModifications? 0 Answers
How to save variables in editor, to use them during play mode 0 Answers
Strategy for saving game 1 Answer