- Home /
PlayerPref or DontDestroyOnLoad for lives
So I'm creating a live system for my game, where when the player dies, the level gets reloaded with Aplication.LoadLevel(), but the amount of lives should stay, so I'm wondering which way of doing this is better practise or is better performance wise? Using a singleton(so it doesn't get duplicates) gameObject containing information about the amount of lives, wich I don't destroy using DontDestroyOnLoad or using PlayerPref to set lives or is there another way of doing this?
Thanks in advance!
Answer by stepan-stulov · Aug 14, 2015 at 07:44 AM
PlayerPrefs is a persistent storage of data. What that means is the values saved there are saved between game restarts. This is a fairly traditional and easy to use place to put your savegames.
DontDestroyOnLoad is (or more correctly: can be used as) runtime storage of data. DontDestroyOnLoad is supposedly a method that tells a game object to not be destroyed on entering another scene. But as far as I know it doesn't do exactly what the name suggests. I think it actually recreates the same object. Which may lead to duplications of game objects and other artefact. From my experience I would avoid DontDestroyOnLoad at all cost. It's a half measure and a very weird hacky method in general.
What should you use? Do you want player lives to persist between game launches? If so — PlayerPrefs or any other available serialization tool (like Thrift for example, or even standard .NET serialization and file I/O). If not — something runtime-only, but also not DontDestroyOnLoad as my humble recommendation.
Answer by StaNov · Aug 14, 2015 at 07:49 AM
From what you are saying it seems that level-reload happens very rarely (like twice a minute for example I guess), so solving performance issues seems unnecessary. Stick with the first rule of optimization.
Putting lives-counter into DontDestroyOnLoad object seems conceptually better. If you have a lot of preferences, you may want to have only preferences in PlayerPref. But if you have only little preferences, it doesn't matter where you put the lives-counter.
Answer by Lainex · Aug 14, 2015 at 10:09 AM
Alright, thanks everyone! I decided to go with PlayerPref, because it does seem more practical and less messy, even though I feel kinda dirty for saving stuff on others computers.
Your answer
Follow this Question
Related Questions
Pick up objects 2 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Is this a good practice for mobile devices? 2 Answers
Destroy fireball on impact 0 Answers
Destroy character controller and instantiate new one 1 Answer