Greetings, fellow gamers! As a game developer, I have been working on an exciting 3D game with rogue-like elements in Unity, and I have come across a crucial question in my development process: How can I persist data between scenes and game sessions?
As any game developer knows, handling data between scenes can be essential depending on the type of game you’re making, and there are different approaches to this challenge. Some developers suggest using PlayerPrefs, while others believe it should only be used to store other things like screen brightness, and so on. I have also come across the suggestion of writing everything into a save game every time a scene changes, among others…
Persist Data Between Scenes With A Global Object
I initially decided to use a global game object that is not destroyed between scenes to handle all the data between scenes. When the game starts, I load a start scene where this object is loaded, and after this, it loads the first real game scene, which is usually a main menu.
While my current implementation is using DontDestroyOnLoad, I wanted to research more before committing fully because I needed to write a save routine to persist information between game sessions, which my current method wasn’t handling.
Persisting Data With A Static Class
After exploring different options, I have come across some interesting ways to handle data between scenes that can also persist across game sessions. One approach is to create a static class that holds data only, allowing easy access to the data from anywhere in the project. This approach makes it easy to manage all variables and data in a single database-like class. This would work but I would still need to write an additional save routine to persist the data between sessions.
Or Using A Singleton Pattern
There is also the singleton pattern, which is easy to set up and use, and allows easy access to the data from anywhere in the project. However, this method requires a lot of boilerplate code to maintain and secure the singleton instance, and there are arguments against its use. I would still need to implement a save routine for persistence between gaming sessions. Here’s a nice post about singletons on Unity Geek, and one on Stack Overflow.
Persist Data Between Scenes Using PlayerPrefs
Another approach is to use PlayerPrefs, which stores data even if the game gets closed, making it easy to manage since Unity handles all the background logic. This approach is useful if you want your data to be stored and passed between scenes and game sessions. The data is stored in plain text and is easily accessible on the user device – but so is most data on the user device!
Using A Custom Save Routine
As mentioned earlier, there is also the option of saving data to a file, which is useful if you need to transfer the file or if you want to be in control of the data saved. However, this method is slow, uses the file system, and has the possibility of data clashes due to poor implementation. The data would still also be in plain text unless you implemented encryption.
As for my game, I have decided to use PlayerPrefs within my next project to persist game progress between sessions as I want the player to be able to exit the game between sessions and seamlessly resume if desired, I would also like a form of basic progression but I haven’t worked out exactly what forms this will present itself in yet. Using PlayerPrefs will make it easy to manage and store data, making it simple for players to continue their progress where they left off, even if they close the game. The data being stored between game sessions isn’t sensitive but editing it would effectively be save game editing.