- Home /
Best practice for using static classes to persist between scenes
I know there are traditionally a few ways of persisting data between scenes (Player Prefs, saving to disk & then loading, static classes, Do Not Destroy, Scriptable Objects). In my situation, I've determined that static is likely my best bet (no need to save between instances, not a Monobehaviour derivative, much of my data is abstract (eg. economy data) so doesn't exist in Monobehaviours and can't reference Scriptable Objects).
However, some of the values I need to store are ones that I want to put protections around (for example, adding a value to a list rather than resetting the whole list). ie.
List<CustomObject> myCustomObjects;
public void addToMyCustomObjects(CustomObject obj) {
myCustomObjects.Add(obj)
}
I'm struggling to work out how to do so whilst restricted to get/set. Is it bad practice/are there reasons why I shouldn't store these values in a normal class during play (where I can put these protections in), and then when moving to a new scene, transfer them across to the static class? Then when returning to the scene, loading them back into their 'normal' classes?
If this is ok, should I be doing this with ALL variables or just the ones that need this mechanism, as a good practice thing?
you are aware that you do not need a complete static class to keep data on scenechange? All static variables are kept.
Only if those scripts are kept/attached to objects, right? I don't need the vast majority of the data, so it'd be a waste of memory to keep them around and active.
Your answer
![](https://koobas.hobune.stream/wayback/20220613032511im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What is the best way of resuming game scene after the menu scene? 0 Answers
How to persist player inventory using dictionary between scenes 2 Answers
Lighting is different after changing scene 3 Answers
Unity3D + Hibernate / Spring? 2 Answers
Persistent data, Save file doesn't come out as hoped 1 Answer