I have some public variables I don’t want to bother having getters and setters for, so they are of course showing up in the Editor. I intend to initialise them at runtime. Is there any problem with just leaving them undefined in the editor?
put this above your variable :
C#
[HideInInspector]
public int p = 5;
Unityscript
@HideInInspector
var p = 5;
HideInInspector will keep them from showing in the inspector, but they still get serialized.
If these are values that shouldn’t even be serialized because they don’t need to be saved. Use ‘System.NonSerialized’:
[System.NonSerialized()]
public int p = 5;
This will both hide them in the inspector as well as not serialize it.
Making the field private also hides them in the inspector, as well as makes them private to the scope of the class. BUT, I have noticed that my private fields still get serialized sometimes (not sure what causes it…), so I still put NonSerialized over pretty much everything I need not serialized.
Thanks, I actually don’t care if they show in Inspector, and the reason I was not wanting to use full property setters was to cut down on lines of code, so I am not too keen on adding the attribute tag, though it’s useful to know, thanks. What I want to know specifically is does it do any harm to have fields that do show in the editor, but not set them there?
Note, you can leave out the field all together, and write a property that implies the field:
public int P {get;set;}
The compiler will take care of everything else, this won’t show in the inspector as well.
Also… “cut down on lines of code”? Write the lines of code to do what you need to do, cutting out lines of code that do tasks that you want to occur is what programming is. If you want it to hideininspector, not be serialized, or be a property… write that line of code! If you don’t want that, then why bother bring it up?
Because I just want to write
public int p;
and set p later on. I want it serialised/saved with the savegame, and I don’t care if it shows in the editor or not. My only question is "is it ok to not set it in the editor to a value? They won’t all be ints, some will be custom types.
I don’t think I ever said I don’t want them showing in the editor, I didn’t ask how to stop them showing. While I am grateful for all help, being snarky to me about something I didn’t ask isn’t helpful to the actual question.
to answer your question then … its perfectly fine to declare it public , set it at runtime and do nothing with it in the inspector
Thank you shaderbytes
If you initialize their value in awake or start then you can ignore the editor values. If you don’t initialize them in start or awake then when you start the game the editor values will override any declared value. For example, if you write public int p = 5; test your scene and then change p to 6, it’s going to be 5 again because the public value in the inspector will still be 5.
If a value appears to be frozen, it’s usually the inspector’s fault.
Ah… so my proposed practice could cause undesirable effects unless I am very careful. So it looks like if I don’t want to risk the Inspector hijacking my saved values, and if I do want them saved, I really have to just put in the effort of having a private variable with a public getter/setter. But then lordofduct was saying private variables don’t always get serialised automatically?
Actually probably an unnecessary question because obviously loading a saved game will come after any Inspector mangling that might happen
And, as I said, if you want something to be not serialized and dealt with in the inspector… you write the lines of code necessary to accomplish that task.