- Home /
[HideInInspector] not serializing variables - how exactly does it work?
I have this private variable in my script:
[HideInInspector] List<GameObject> m_spawnedCreatures = new List<GameObject>();
It is changed (I add items to the list) in an editor-time method that I call by pressing a button on a custom editor for this component. The problem is that at runtime, in the Update() of this same class, the list is always empty.
The documentation says that HideInInspector
"Makes a variable not show up in the inspector but be serialized.", so I expect it to preserve the value from editor to runtime - so why then doesn't it?. This of course works with [SerializeField],
but I need my variable hidden from GUI.
It also works when I put both attributes:
[SerializeField] [HideInInspector]
, but then, sometimes I get the exception when reading variable value at runtime : "m_spawnedCreatures is not assigned". Which is fair, it was not assigned from editor. I guess this is a dirty workaround
Answer by Namey5 · Oct 24, 2020 at 01:12 AM
If you want the variable to be serialized, then it must either be public or have the SerializeField attribute. If you don't want it displayed in the inspector, then you can either use HideInInspector to force the default inspector to ignore it, or make your own custom inspector that doesn't display that variable. I've seen a few instances of the 'unassigned' error with serialized non-public fields, but that is mainly to do with Unity's built in reference types (i.e. Texture2D) - the workaround for those is to use the default keyword;
[SerializeField, HideInInspector] Texture2D m_Texture = default;
Not sure if the same would apply for a list (could also be a generics thing), but worth a shot.
Answer by Talmagett · Dec 31, 2020 at 04:55 PM
Have a same issue, I think [HideInInspector] work with only List not GameObject
Your answer
![](https://koobas.hobune.stream/wayback/20220613014357im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to correctly use [SerializeReference] on generic types 0 Answers
serialize static fields 1 Answer
How to serialized a variable in a c# method? 1 Answer
Can a GameObject be serialized? 1 Answer
Select a type from the inspector 0 Answers