- Home /
[C#]Persistent Saving and Loading
Alright, so basically I have this slight issue that I'm trying to get a fix for. I'm using a GameObject called GameControl that I have learned from the Unity Tutorial guy. What I'm trying to do is be able to edit the options after they have been loaded. I can get them to save and load just fine. I had to switch the options up differently for the Singleplayer and Multiplayer mode because they have different save and load data. Now when I save/load this data back. I'm having troubles getting them to edit after loading, because it seems to be continuously loading the saved data, so if I were to make a change, it alters back to the saved data.
When I was just following along, I had this when the options were both the same and I used
void Start() {
GameControl.control.Load();
}
Which seemed to work just fine, but now I have two different load files SP Options and MP Options. And I just don't know where to put these files, I thought maybe in my Switch Operation where I have it to load when I click Singleplayer, or when it loads Multiplayer at click. But this is where I have the continuous loading problem. And when I go to edit the options in the Window, it reverts back, please help me understand where to put these loads. I don't want to have a load button, I don't like the idea of that, I want it to be an automatic load, when you click Singleplayer (get singleplayer load files); and Multiplayer (get multiplayer load files).
void OnGUI() {
GUI.skin=mainSkin;
menuSelection=GUI.SelectionGrid(new Rect(Screen.width*.025f,Screen.height*.025f,buttonW,buttonH), menuSelection, button, 1);
switch(menuSelection) {
case 0:
windowRect=GUI.Window(0,windowRect,SPOptionsWindow,"Singleplayer Options (Pre-Game)");
GameControl.control.LoadSP();
break;
case 1:
windowRect=GUI.Window(1,windowRect,MPOptionsWindow,"Multiplayer Options (Pre-Game)");
GameControl.control.LoadMP();
break;
case 2:
LeaderboardsWindow();
break;
default:
break;
}
Here is where I have it loading the Load files; however I am unsure this is where it's supposed to be unless there is more code to make it stop running after the first time it loads. I've also tried to load it in the SPOptionsWindow(); and MPOptionsWindow(); but I get the same result.
Answer by jeThomas · Oct 20, 2014 at 04:21 AM
Ahhhhh, long time fix! So basically what I did to correct this was to add more code in the GameControl.cs file. Although some features aren't the same, all I had to do was just recreate the same types with different naming conventions. More code, however it definitely works. However temporarily. Thinking long-term, however, I'm not sure if this is the exact fix that I would appreciate to have.
Problems may occur: •The actual options taking place would require more code to implement. •Maybe I won't be able to implement more code, but I would have to change it back to being the same?
These I don't really know until I reach that bridge, but however, it's a fix for now, and if there are any other answers to this, please throw your idea out there :)
I'd be very interested to know more about how to resolve this issue as I'm going to get the same problem once I add multiplayer. What I was thinking, is to maybe have two gamecontrol gameobjects ins$$anonymous$$d of one, with both starting off as inactive. The menu could perhaps make the relevant gameobject active? It might make managing the persistant data from a file easier if it is managed by different scripts/objects and less issues with required code to segregate SP vs $$anonymous$$P.
Yeah, to be honest I don't have it set up like my solution. I actually am trying something different. Because I was thinking that if I click the button Singleplayer, all you can do is pull up the options pre start. And when you click $$anonymous$$ultiplayer I needed it to pull up two more buttons (or maybe more, but right now I only thought of two). So I actually have the same issue but I have two Switch Cases where I throw the
default: GameControl.control.LoadSP() break;
and the other Game.control.Load$$anonymous$$P() in the other break, and to be honest there's some weird stuff happening there when switching between the two lmao. Buttttt it's okay for now, because I actually have it just loading the $$anonymous$$P files, because I have both set to save to Save$$anonymous$$P.dat and whatever is missing from the Single Player that the $$anonymous$$ulti Player has doesn't show up. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613165448im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[Solved] Built in GUI continuously Loads through different Options. 1 Answer
in game menu for load, save, quit to main menu scene and volume. also a question on opening scenes 1 Answer
Serializer deletes saved info on game restart 1 Answer
Unity Not Loading 0 Answers
SerializationException: Type UnityEngine.UI.Button is not marked as Serializable. Solution 1 Answer