Is it ok to use a singleton gamemanager to persist player's data?

Hi, in my game i have a singleton gamemanager script that i store player’s health and stuff to persist between scenes and it works perfectly with no problems at all, but i’m afraid that it’s not a good practice and might face problems later. like can i have save and load system with this or do i need to use scriptable objects? i have tried using them, but i only know little about them, and my method seems easier for me.

sorry i’m really just a beginner and overthinking everything :sweat_smile:

Singletons are often considered bad practice, but if it solves the problem then it’s ok to use them as long as the rest of your code doesn’t become overly dependent on it.

The main issue with singletons is that they are extremely useful, beginner programmers overuse them ending up with 20 managers and a huge spaghetti that is nearly impossible to debug. To avoid this keep the SOLID principles in mind and you’ll be ok.

If you don’t want to “manually” save and load the data I don’t recommend using Scriptable Objects. I always thought they were persistent, but they aren’t, if you change them during run time there’s a chance that data won’t persist once you load a new scene which is annoying. I’m not 100% if that is a Unity bug or something they changed in newer versions, because I remember them being persistent, but a few months ago, with Unity 2021.3.11f1, I came across that annoyance.

You can have a full loading and saving system with a singleton, so don’t worry too much about that, you’ll just have to be really mindful as to when the game saves and loads, and also make it really clear to the player.

1 Like

thank you, really appreciate the reply! and yeah i don’t planning on using a lot of singletons, just one or two and let them manage everything in the game.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms