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?
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.
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.
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.