- Home /
Persisting data between scenes: can't find an answer
I've read all the solutions for data persistence, and I understand them. The problem is I don't know which one is the best for my game.
I have a RPG with party members, I need to keep the party members between scenes: to access their inventory, their stats, to spawn them in battles, etc. Sounds simple. I started implementing dontdestroy with a singleton but I find it very cumbersome: so I thought, why not write all this to JSON files and read from them when needed?
Ppl say this will be slow, because you'll have to read from files every time you open the inventory, stats, go in battle, etc. On the other hand, this just saves headaches. So will this be too slow, or do you have a better solution?
Answer by SuperScience · Oct 31, 2018 at 04:27 AM
Hi!
The usual mechanism is to have a gameobject (or objects) store the data and set that object to DontDestroyOnLoad.
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Or you can use statics too, but then you need to be careful to clear the statics if the player loads. The advantage of DontDestroyOnLoad is that you can still destroy it manually if the player does a quickload or whatever. You can destroy, recreate from save, load the scene, and done!
Hope this helps
Answer by A_Lego · Oct 31, 2018 at 04:52 AM
You can also just save the data before the scene ends then, once the next scene loads, read and re-store the data on that scene. You'll only need to read and write to the file once in each scene transitions and it won't be too cumbersome on the game play itself! Hope this helps
Your answer
Follow this Question
Related Questions
PlayerPrefs not saving in build 1 Answer
How to load data from this serializable class? 1 Answer
saving array to bin file 0 Answers
Make changes in prefabs persistent. 2 Answers
Get Asset at runtime by its ID 0 Answers