Save all variables from a class

Hi all,

It seems I have a quite specific problem: we just migrated our game to Unity/C#. It is a text based sandbox game with huge content, vast amount of text. The game is updated monthly, for 2,5 year now.

We use about 2000 variables at the moment, and it grows monthly by 50-100.

The whole game is in a gigantic “partial class” made of about 6000 .cs files, along with the definition of these 2000 variables.

All solutions I could find in the forums contained an intermediate class to duplicate the variables and build up the saveable string of them. That is okay for 5-10 variables, but in this case this seems not the best way, espacially because the list of the variables expands every month.

So, I need an script that automatically gets all the variables in this class. I’m not yet expert in this type and property things in C#, they are not very intuitive for me.
I tried some scripts with getProperties but none of them gave me a list of my 2000 variables as I expected, only Unity-related things like “transform”, “tag” and such.

So if I have had a script that iterates through my variants, I could get their values, and write them in a file.

Unity save-load seems so easy, yet it is very complicated :slight_smile:

Anybody could help pls? :slight_smile:

20 minutes later…
I found the solution :smile:

Type t = this.GetType();
foreach (var f in t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
Debug.Log(f.Name);
}

1 Like