Save Data Doubled
I have been following this saving and loading tutorial using XML, I have spent all day trying to fix an issue I am having. I know that it will be hard to diagnose, but any help would be much appreciated.
https://www.youtube.com/watch?v=Y8Di-Q6qpU4&list=LLyz8M113feE2UY5kwTDs2gA∈dex=4
The issue I am having is that I can save, then load, but when I save again, my save data is doubled, resulting in duplicates of my objects. I have determined it is not a filemode issue, because when I save multiple times, it does not double multiple times, only once. And I have tried filemode.Open, and filemode.Create, and gotten the same results. I think it is something with the ActorContainer saving objects from the first save that were deleted when loading, but that did not seem to be the issue either, due to issue occurring even if I reloaded the scene each time I saved. I am really at my wits end with this, and honestly I am not expecting to have this answered, I just cannot think of anything else to try.
I FIXED IT, to anyone else following this tutorial, to fix this issue, run ClearActors(); before and after OnBeforeSave(); and add public static void RemoveActorData(ActorData data) { actorContainer.actors.Remove(data); } to your SaveData.cs and also, on your actor component, add a void OnDestroy(), and use SaveData.OnBeforeSave += delegate { SaveData.RemoveActorData(data); }; ins$$anonymous$$d of SaveData.OnBeforeSave -= delegate { SaveData.AddActorData(data); };
for both OnDestroy() and OnDisable(),
this fixes the issue of the actors not being removed hence "doubling" data. Also I suggest deleting all of your actor objects BEFORE loading new ones, example : loadButton.onClick.AddListener(delegate { DeleteAll(); }); BEFORE loadButton.onClick.AddListener(delegate { SaveData.Load(dataPath); });
and having a DeleteAll () function just search for all objects with the actor component and destroy them.
Answer by GHM_Games · Jul 11, 2016 at 12:08 AM
If it is a file error, is there a way to erase the data from my file before it is doubled, hence making the "doubled" data singular?
I know I won't get an answer for this but I just want to rant for a second here, I downloaded my tutorial's project and tested it out and got the exact same error. You cannot save in a loaded game, if you do, everything is doubled. This is a huge error and practically ruins the whole function of saving due to the ability to only save once. Did they not notice this? Unity also has documentation that uses the same code and most likely has the same issue? Why advertise this as a working saving solution, it doesn't work at all! I have tried for two days straight now anything I can think of and I still get the same issue. It is extremely frustrating and a huge waste of my time, I am very disappointed.
Your answer
