What is the usefulness and consequence of not serializing something (using @System.NonSerialized)?


So I gave it a try by writing out a simple script:

var i : int = 5;
var h : int = 3; //In my inspector, I changed it's value to 1

function Update () {
	print("i: " + i);
	print("h: " + h);

And then console prints: i = 5 and h = 1.

I understand that @NonSerialized hides a variable in the inspector and I understand the concept of not serializing something (i.e. the object won’t be converted to bits that can be stored by the program). However, from the little experiment above, I don’t really see how not serializing something can be useful and what consequence this has. Could someone please enlighten me on this subject?

Why not ask “why serialize everything?” - Serializing everything adds more unnecessary over-head.

I understand that @NonSerialized hides a variable in the inspector

NonSerialized doesn’t hide a variable, it just makes it ignored by the serialization system thus as a side effect hidden.

There’s always variables that you don’t want serialized, usually private ones. When a variable is private, it’s ignored by the serialization system so you don’t need to mark it with NonSerialized.

I’ve never really come to a practical situation where I had to use NonSerialized