- Home /
Save Publict Static Variables
I have a very large Class of public static variables (used for A customizable spaship the player can build) and they are not in an array.
I need to save these variables so that the player doesn't have to rebuild the ship every time they exit and restart the game.
I am very confused about this. I just want a simple way to write the value of all the variable in the class to a file, and also to load the file to reset the variable back to what is saved. Here is a (VERY) small snip-it of the class I want to save.
(keep in mind this class has thousands of variables in this class so I need a way to save that will not require me to write a line of code for each and every variable)
(also C# please!!!)
//The parts that make up the players ship
public class Ship
{
public static GameObject hul;
public static GameObject aet1hul;
public static GameObject inp1hul;
public static GameObject shi1hul;
public static GameObject win1hul;
public static GameObject win2hul;
public static GameObject win3hul;
public static GameObject win4hul;
public static GameObject win5hul;
public static GameObject win6hul;
public static GameObject win7hul;
public static GameObject win8hul;
public static GameObject aug1hul;
public static GameObject aug2hul;
public static GameObject aug3hul;
public static GameObject aug4hul;
public static GameObject aug5hul;
public static GameObject aug6hul;
public static GameObject aug7hul;
public static GameObject aug8hul;
//...ect...ect...ect...
}
The first thing I'm thinking of is THIS, but it will not work for game objects, only basic variables like int, double or string. So if the ship is a combination of limited elements, you can save them with int vars, and map them to an enum for example.
Is there a way then to record the entire script in its current form for reloading?
Why would you want to record an entire script ? It will not record the value of the variables.
I was hoping there is a way to record it with it's current changes. including what the variables current value is. I am rather new at this...
PlayerPrefs.SaveAll$$anonymous$$yStuffTheWayIwantIt() ;
...see if that works. If it doesn't, you're going to need to make your own save/load method(s). Use arrays, also, ins$$anonymous$$d of making numbered variables like you have it. GameObject[] augHul = new GameObject[8] ; would be a lot better than 8 different variables with their index numbers included in their names. Same goes for GameObject[] winHul ; and whatever other of the not-shown variables you mention that could be thrown together into arrays. Then you'd just loop through them to do your save/load stuff ins$$anonymous$$d of writing thousands upon thousands of save/load lines for thousands of variables.
Answer by frarees · Jun 28, 2014 at 04:58 PM
Unity won't serialize static variables on your scripts, as stated here. I recommend you to follow a singleton pattern on that behaviors (e.g. this) to save all your data or use the PlayerPrefs class to store it instead.
Your answer
![](https://koobas.hobune.stream/wayback/20220613152727im_/https://answers.unity.com/themes/thub/images/avi.jpg)