I have a class containing my settings/player stats. How would I serialize the whole class at once in order to save load?

So, I have a few scripts which serve as classes containing settings and player stats. I know how to save and load data by referencing every variable in a class one by one, but I want to save and load a whole class WITH its values at save time through serialization and binary formatting (you know, the classic). Now, I read one question which was answered that the class should be loaded as a game object, but I have no idea what to do… Do I reference the game object holding it and then save/load it? I have the player stats script on my character controller, so I don’t think that loading a new game object from a file would be what I want, as I need it to copy over to that specific player game object.

In essence, let’s say I have this for now:

public class SettingsScript : MonoBehaviour
{
	public GUISettings GUISettings;

	[Header ("Display settings")]
	[Range (24, 144)]
	public int FPS;

	[Range (240, 2160)]
	public int yRes;

	public short RatioMode;
	public string[] Ratios;
	public short multX, multY;
	//0 - 4:3, 1 - 16:9, 2 - 16:10, 3 - Custom
}

and on run time I have set some values. This script persists on a special game object that only holds modules like a transition effects script, GUI settings, map information container, etc. I would want to save this whole class in a file and then also load it all at once when loading, so with the least amount of hard-coding.

This is what ScriptableObjects are for:

You do not want to use Scriptable Objects. Scriptable Objects are essentially config files that should not be changed at runtime. You should have any information that you want to persist be put into a class that does not inherit from MonoBehaviour. For instance, if I have a Player class that inherits from MonoBehaviour that handles controller input and stuff… It is what many who program consider a View. It’s the visual side of a program. But it is not a Model, which is the data side of a program. So I would have another class called PlayerData that does not inherit from MonoBehaviour. PlayerData will hold nothing but pure data. It should not have any references to MonoBehaviour or ScriptableObjects inside it either, because they don’t serialize. Then, when you want to save the data, you serialize the entire PlayerData file. When you want to load it, you load the PlayerData file. This one file will hold all of the information you have for each player. Here’s the benefit of doing it this way. First off, you don’t get errors about trying to serialize a Game Object or Scriptable Object. Secondly, if you do it right, all you need to do is instantiate the Player prefab, access the Player script on it, and load the Player Data and your player will be completely as it was. In your case, all of those Display Settings will need to go into a class of their own so it to can be serialized if it changes. The only thing you should have on your MonoBehaviour derived class is variables that are being tested, or those that differ from other instances of the same class and stored in different prefabs, but not variables that can change at runtime and need to persist. For instance, if I create an AI class, I might have AI difficulty be on the MonoBehaviour, and then save a prefab of AI for Easy, Medium, and Hard, with that value being different for each. But it doesn’t change at runtime, especially not in a way that needs to persist. Hopefully this helped, but I have more reading you can do on the subject that can be found on a post in my blog which will even walk through the way you can serialize your data: Serialization with Json.NET in Unity 3D – GrayMatter Tutorials