Is it possible to stop Unity from showing public Variables in the Inspector?
Some variables need to be public but I want to set their values inside the code, not in the inspector. This becomes a huge pain in the ass when tweaking and debugging certain values. The only time I need something in the Inspector is when dragging a Prefab from an asset Folder to a public GameObject.
I guess the easy way around this would be to set the values in the Start function. However I'd rather not have them show up in the Inspector.
Answer by tanoshimi · Jan 06, 2017 at 07:00 PM
Use [HideInInspector]:
[HideInInspector] public float thisVarWontShowInInspector;
https://docs.unity3d.com/ScriptReference/HideInInspector.html
Note that "HideInInspector" just "hides" the variable but it's still serialized and will keep it's value. Once hidden you can't edit the value anymore, just with a custom inspector. This is most likely not what you want. Using System.NonSerialized will hide the variable and prevent it from being serialized. Strangely the documentation page is named "NonSerializable" which doesn't even exist. The NonSerialized attribute isn't a Unity class but comes with the .NET / $$anonymous$$ono framework.
Answer by elenzil · Jan 06, 2017 at 11:28 PM
another approach is to make it a private variable with a setter/getter.
Your answer
Follow this Question
Related Questions
How do I bind the value of a public variable from the inspector to some other variable/constant? 0 Answers
Public InputController class not showing on inspector 0 Answers
public int variable not updated in inspector for two scenes in Unity 0 Answers
Public Arrays reset themselves 1 Answer
GameObject fields evaluate to null, but they are actually assigned and working 1 Answer