I have a couple of ideas, but I’d appreciate input from a Unity professional. Let’s say I need a game object named “Database” to store crucial data for various game objects across different scenes.
The first idea is to create a singleton game object and use “DontDestroyOnLoad” so that other game objects can access the Database throughout the game. However, this means the Database remains present even in scenes where it might not be needed. Also, it could involve having multiple scenes open simultaneously, which I’m unsure if it’s an issue. Additionally, I’d have to ensure I open Scene A first to create the Database GameObject before accessing other scenes. Is there a way to automatically create the Database regardless of the scene I open.
The second idea involves saving necessary data locally and loading it into specific game objects when required.
Thank you for your insights! I’m open to other suggestions if these approaches might have drawbacks.
In this instance, just have this database game object self instantiate and DDoL itself when first accessed (aka, lazily load it). Don’t tie it to a scene, just make it initialise itself.
Don’t see it being an issue being around for the lifetime of the application. Usually this kind of stuff you want available from beginning to end of the application.
This is also a reasonable option, though I would only use it if the data needs to be saved and loaded infrequently. Such as the data that represents a level, particularly if we’re working with procedurally generated levels.
Other options include:
Singleton pattern scriptable object, which can also just be referenced directly when needed
Or scriptable objects that you use to communicate data between scenes; these often need to be reset when entering/exiting play mode, however
Static class that acts as your main API access point
Most often a project will contain multiple different solutions to this problem, depending in the individual use cases.
Thank for the reply !
the thing is the data I mentioned need to be saved and loaded at some point (think of it as a “what level player is in” data) so I’m still thinking if the saving solution is better (because I need to implement the saving system anyway) but like you mentioned I need to save and load it frequently which is kind of a pain…
what do you mean by “just make it initialise itself.” ? I’m kidn of confused here but I believe this is what I’m looking for. can you at least give me a documentation about this or example I would really be appreciated.
You can probably just load the data when it’s needed, work with the live data during the lifetime of the level, and then save it to disk when no longer using the level.
I wouldn’t be saving and loading data just to transfer information between scenes. That’s definitely not required.
Basically, when it’s first access, it just instances itself into existence, initialising itself at the same time.
Rough example:
public class AutoSingletonExample : Monobehaviour
{
private static AutoSingletonExample _instance;
private SomeDataObject _someData;
public static AutoSingletonExample Instance
{
get
{
if (_instance == null)
{
_instance = CreateInstance();
}
return _instance;
}
}
private static AutoSingletonExample CreateInstance()
{
var go = new GameObject("Singleton");
DontDestroyOnLoad(go);
var component = go.AddComponent<AutoSingletonExample>();
return component;
}
private void Awake()
{
_someData = // load data, etc
}
}